Python 停止while语句

Python 停止while语句,python,if-statement,while-loop,Python,If Statement,While Loop,在我的《计算机科学导论》课程中,我被指派编写一个程序,该程序将打印出一个要求用户输入20-99的数字。我已经能够做到这一点,并创建了一个错误消息,如果用户没有输入一个在此范围内的数字。我遇到的问题是,当输入一个超出范围的数字时,会显示错误消息,但由于某些原因,程序会继续,并仍然打印出一个英文数字。我一直在试图找出如何让程序在出现错误消息时停止,但我一直无法找到答案。这是我目前拥有的 a=int(input('Pick a number between 20 through 99:')) b=a

在我的《计算机科学导论》课程中,我被指派编写一个程序,该程序将打印出一个要求用户输入20-99的数字。我已经能够做到这一点,并创建了一个错误消息,如果用户没有输入一个在此范围内的数字。我遇到的问题是,当输入一个超出范围的数字时,会显示错误消息,但由于某些原因,程序会继续,并仍然打印出一个英文数字。我一直在试图找出如何让程序在出现错误消息时停止,但我一直无法找到答案。这是我目前拥有的

a=int(input('Pick a number between 20 through 99:'))

b=a//10

c=a%10

while a<20 or a>99:

    print('Error, enter number between 20 and 99:')
    break

while a>20 or a<99:

    if b==2:
        print('The number is Twenty',end=' ')
    elif b==3:
        print('The number is Thirty',end=' ')
    elif b==4:
        print('The number is Fourty',end=' ')
    elif b==5:
        print('The number is Fifty',end=' ')
    elif b==6:
        print('The number is Sixty',end=' ')
    elif b==7:
        print('The number is Seventy',end=' ')
    elif b==8:
        print('The number is Eighty',end=' ')
    else:
        print('The number is Ninety',end=' ')
    if c==1:
        print('One')
    elif c==2:
        print('Two')
    elif c==3:
        print('Three')
    elif c==4:
        print('Four')
    elif c==5:
        print('Five')
    elif c==6:
        print('Six')
    elif c==7:
        print('Seven')
    elif c==8:
        print('Eight')
    else:
        print('Nine')       
    break

你想要的第二个条件,而不是或


也要使用if,因为如果不使用if,它将无限循环。这里需要一个if语句;而对于你打算重复的事情


另外,请注意,>20或将break语句放在while循环中。这意味着当您到达该语句时,您将离开while循环。因此,无论发生什么,函数都将离开循环。while循环不正确或不合适的一个好迹象是,如果在末尾调用break。这里有一个更好的方法

while True:
    a=int(input('Pick a number between 20 through 99:'))
    if a > 20 and a < 99:
        break;
    else:
        print("Error, enter number between 20 and 99")

首先,感谢你诚实地说这是家庭作业。您可以使用条件if语句来测试输入的变量是否介于20和99之间,而不是循环。您有一个带有无条件中断的循环。这基本上只是一个if语句。如果你真的想继续要求一个新号码,你需要在循环中这样做。看起来您需要回顾一下教科书中关于循环和输入检查的部分。end=在python3中不是语法错误。当您使用end=并使用print时,它会在末尾放置一个空格,而不是换行符!非常感谢。但是end=的原因是数字的第一部分和第二部分在一行,而不是两行。谢谢;我们仍然使用Python2.7。
while True:
    a=int(input('Pick a number between 20 through 99:'))
    if a > 20 and a < 99:
        break;
    else:
        print("Error, enter number between 20 and 99")
while True:
    a=int(input('Pick a number between 20 through 99:'))
    if a > 20 and a < 99:
        break;
    else:
        print("Error, enter number between 20 and 99")

b=a//10

c=a%10

if b==2:
    print('The number is Twenty',end=' ')
elif b==3:
    print('The number is Thirty',end=' ')
elif b==4:
    print('The number is Fourty',end=' ')
elif b==5:
    print('The number is Fifty',end=' ')
elif b==6:
    print('The number is Sixty',end=' ')
elif b==7:
    print('The number is Seventy',end=' ')
elif b==8:
    print('The number is Eighty',end=' ')
else:
    print('The number is Ninety',end=' ')
if c==1:
    print('One')
elif c==2:
    print('Two')
elif c==3:
    print('Three')
elif c==4:
    print('Four')
elif c==5:
    print('Five')
elif c==6:
    print('Six')
elif c==7:
    print('Seven')
elif c==8:
    print('Eight')
else:
    print('Nine') `enter code here`