简体   繁体   中英

How to use continue statement in a loop?

This is a code that takes a list of numbers from the user and computes the smallest and largest till he enters "done". Anytime the user enters an invalid input, this iteration should be skipped. I used continue in the loop to do this, but it doesn't work as I expect.

largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done" : break
    try:
        isinstance(num,int) == True
    except:
        print('Invalid input')
        continue
    if largest is None :
        largest = num
    elif num > largest :
        largest = num
    if smallest is None :
        smallest = num
    elif num < smallest :
        smallest = num
print("Maximum is", largest)
print("Minimum is", smallest)

for instance, when using this list of inputs: 1 ; 2 ; alex ; 9 ; and 7 , this is the output that I've got:

Maximum is Alex
Minimum is 1

This should work, except you are never actually converting the input to an int . I believe the return value from input will always be a string. That doesn't mean it can't be converted to an int , but it will be the string "42" instead of the int 42 at first. I would try:

Instead of isinstance(num,int) == True do int_input = int(num) . Your try/catch will catch if this doesn't work if num wasn't an int-like input. Note that your original isinstance() call will never raise an exception, it will just evaluate to True or False , so it will always proceed, which is why you are getting your strange results. Your continue is now in the right place. Then use int_input instead of num for the rest of your code, as you'll want to be dealing with the int, not the string input.

Here's an example:

largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
        break
    try:
        int_input = int(num)
    except:
        print('Invalid input')
        continue
    if largest is None :
        largest = int_input
    elif int_input > largest :
        largest = int_input
    if smallest is None :
        smallest = int_input
    elif int_input < smallest :
        smallest = int_input
print("Maximum is", largest)
print("Minimum is", smallest)

Firstly, the statement isinstance(num, int) == True checks whether num is an integer, then throws away the answer. It never actually causes an error (unless the isinstance function itself has an internal error).

You probably want to use an if statement:

if not isinstance(num, int):
    print('Invalid input')
    continue

Secondly, as others have noted, the input() function will always return a string; when you gave the program 1 ; 2 ; alex ; 9 ; and 7 , it found the maximum and minimum in alphabetical order — or, rather the computer variant of alphabetical order, in which numbers come before letters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM