简体   繁体   English

陷入无限循环。 Python

[英]Stuck in an infinite loop. Python

I am trying to make this code run so that when I enter 'done' it break me out of the loop.我试图让这段代码运行,这样当我输入“完成”时,它就会让我跳出循环。 The issue is I put a try/except to try and catch anything that isn't a number and feed back and error message.问题是我放了一个try/except来尝试捕获任何不是数字的东西并反馈和错误消息。 This catches before the if statement that would break me out of the loop and instead feeds back an error message and catches me in an infinite loop.这会在 if 语句之前捕获,这将使我脱离循环,而是反馈错误消息并将我捕获到无限循环中。 I've tried moving the try/except after all the if/else statements but then the string gets placed inside the value for maximum and this is not what I am trying to accomplish.我已经尝试在所有if/else语句之后移动try/except ,但随后字符串被放置在最大值的值内,这不是我想要完成的。 Is there a way for me to get the try and except to run but still allow me to pass a 'done' command to exit the loop?有没有办法让我尝试并运行,但仍然允许我通过“完成”命令退出循环?

largest = None
smallest = None

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

    print(num)

print("Maximum", largest)
print("Minimum", smallest)

first test for done and then convert to float首先测试done ,然后转换为浮动

And you can simplify the tests你可以简化测试

The program should work if you enter none or one number如果您不输入任何数字或输入一个数字,该程序应该可以工作

    num = input("Enter a number: ")
    if num == "done" :
        break
    try:
        num = float(num)
    except:
        print('Invalid Input')
        continue
    largest = largest or num
    smallest = smallest or num
    largest = max(num, largest)
    smallest = min(num, smallest)

Change this:改变这个:

try:
    num = float(num)
except:
    print('Invalid Input')
    continue

if num == "done":
    break

to this:对此:

try:
    num = float(num)
except:
    if num == "done":
        break

    print('Invalid Input')    
    continue

By putting the if inside the except , you allow the loop to break before it can continue .通过将if放在except中,您可以让循环在continue之前break

The reason is that you're using checking if the input is a float before you're checking for "done" so when it's actually "done" it just continues before it checks for it.原因是您在检查"done"之前使用检查输入是否为浮点数,因此当它实际上"done"时,它会在检查之前继续。

I would fix it this way.我会这样修复它。

largest = None
smallest = None

while True:
    num = input("Enter a number: ")

    # check if it's done first
    if num == "done" :
        break

    try:
        num = float(num)
    except:
        print('Invalid Input')
        continue

    elif largest is None:
        largest = num
    elif smallest is None:
        smallest = num
    elif largest < num:
        largest = num
    elif smallest > num:
        smallest = num

    print(num)

print("Maximum", largest)
print("Minimum", smallest)

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

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