Home > Enterprise >  script that prints smallest and largest number from input
script that prints smallest and largest number from input

Time:03-12

I have a problem with this code because it puts "done"(phrase to end the loop) as smallest input

largest = None
smallest = None
NumList = []
while True:
         value = input("enter a number:")
         NumList.append(value)
         if value=="done":
             break
try: f = float(num)
except :
    print("Invalid input")


print("Maximum is",min(NumList))
print("Minimum is",max(NumList))

CodePudding user response:

There are mainly two mistakes:

  1. When the user inputs 'done', you are appending 'done' before checking whether it is 'done'. So you have 'done' in your list even though you don't need it.

  2. Conversion of a string to a float is out of place.

The following is a working modification:

nums = []
while True:
    val = input("Enter a number: ")
    if val == "done":
        break
    try:
        nums.append(float(val))
    except ValueError:
        print(f"Invalid input: {val}")

print(f"Maximum is: {max(nums)}")
print(f"Minimum is: {min(nums)}")
  • Related