简体   繁体   English

如何在循环中使用 continue 语句?

[英]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.我在循环中使用continue来执行此操作,但它没有按我的预期工作。

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 ;例如,当使用这个输入列表时: 1 2 ; 2 ; alex ; alex 9 ; 9 ; and 7 , this is the output that I've got:7 ,这是我得到的 output :

Maximum is Alex
Minimum is 1

This should work, except you are never actually converting the input to an int .这应该有效,除非您实际上从未将输入转换为int I believe the return value from input will always be a string.我相信input的返回值将始终是一个字符串。 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.这并不意味着它不能转换为int ,但它最初将是字符串"42"而不是 int 42 I would try:我会尝试:

Instead of isinstance(num,int) == True do int_input = int(num) .而不是isinstance(num,int) == Trueint_input = int(num) Your try/catch will catch if this doesn't work if num wasn't an int-like input.如果 num 不是类似 int 的输入,则如果这不起作用,您的 try/catch 将捕获。 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.请注意,您最初的isinstance()调用永远不会引发异常,它只会评估为TrueFalse ,因此它将始终继续进行,这就是您得到奇怪结果的原因。 Your continue is now in the right place.您的 continue 现在位于正确的位置。 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.然后对代码的 rest 使用int_input而不是num ,因为您需要处理 int,而不是字符串输入。

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.首先,语句isinstance(num, int) == True检查num是否为 integer,然后丢弃答案。 It never actually causes an error (unless the isinstance function itself has an internal error).它实际上永远不会导致错误(除非isinstance function 本身有内部错误)。

You probably want to use an if statement:您可能想要使用if语句:

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

Secondly, as others have noted, the input() function will always return a string;其次,正如其他人所指出的, input() function 将始终返回一个字符串; when you gave the program 1 ;当你给程序1 ; 2 ; 2 ; alex ; alex 9 ; 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.7 ,它找到了按字母顺序排列的最大值和最小值——或者,更确切地说,是字母顺序的计算机变体,其中数字在字母之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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