Home > Mobile >  My python loop is either freezing up or running once
My python loop is either freezing up or running once

Time:12-18

The purpose of this program is to have a user input numbers until they enter done, at which point the program outputs the maximum & minimum numbers. If the user enters something that is not a number, the program says "invalid input" and skips to the next time the loop runs. I have a try/except as well. This program is written in Python 3. when I run without the continue at the bottom, my screen freezes & I have to restart. With it, I get a pop-up saying SyntaxError: bad input on line 25.

        largest = 0
        largest = float(largest)
        smallest = 0
        smallest = float(smallest)
        num = input('Enter a number: ')
        num = float(num)
        while True:
          if num == "done":
                break
            try:
                float(num)
            except:
                print('Invalid Input')
                continue
            if num > largest:
                largest = num
     
           if smallest == 0:
                smallest= num
    
            if smallest > num:
               smallest = num
            print(num)
            continue
        print("Maximum is", largest)
        print("Minimum is", smallest)

CodePudding user response:

Your input is outside of your loop so it only asks once and since it doesn't ever change its value to "done", it loops forever.

largest = 0
largest = float(largest)
smallest = 0
smallest = float(smallest)
# num = input("Enter a number: ")

while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        num = float(num)
        float(num)
    except:
        print("Invalid Input")
        continue
    if num > largest:
        largest = num

    if smallest == 0:
        smallest = num

    if smallest > num:
        smallest = num
    print(num)
    continue

print("Maximum is", largest)
print("Minimum is", smallest)
  • Related