I've made some corrections to your code. Please see the modified version below:
largest = None
smallest = None
lst = []
while True:
num = input("Enter a number: ")
if num == 'done':
break
try:
fnum = float(num)
lst.append(fnum)
if largest is None or fnum > largest:
largest = fnum
if smallest is None or fnum < smallest:
smallest = fnum
except ValueError:
print("Invalid input. Please enter a valid number.")
if lst:
print("Maximum element in the list is:", max(lst))
print("Minimum element in the list is:", min(lst))
else:
print("No numbers entered.")
Here's what I changed:
Moved the declaration of lst outside the loop so that it accumulates all the numbers entered by the user.
Inside the loop, I converted the input num to a float (fnum) and added it to the list lst.
Added checks to update the largest and smallest values accordingly based on the input number.
Added a check at the end to ensure that the list is not empty before printing the maximum and minimum values.
Improved the formatting of the output statements for clarity.
Now, the program should repeatedly prompt the user for numbers, store them in a list, and finally print the largest and smallest numbers after the user enters 'done'. If the user enters an invalid input, it will display an appropriate message.