停止一段时间的陈述

时间:2015-09-16 22:13:00

标签: python if-statement while-loop

我已经在我的计算机科学课的介绍中编写了一个程序,该程序将打印出一个要求用户输入的数字(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

3 个答案:

答案 0 :(得分:0)

您想要的第二个条件and不是or

while a>20 and a<99:

也使用if,因为如果你不

,它会无限循环

答案 1 :(得分:0)

你很困惑&#34;而#34;与&#34;如果&#34;。你需要一个&#34; if&#34;声明在这里; &#34;而&#34;是为了你想重复的事情。

另请注意,&gt; 20或&lt; 99 总是为真;任何数字都是一个或另一个。我相信你想要一个&#34;和&#34;在这里,这使得这只是&#34;否则&#34; &#34;如果&#34;言。

最后,我不确定你要用&#34; end =&#34;做什么?在你的第一批印刷报表中。这是语法错误。

答案 2 :(得分:0)

您已将break语句放在while循环中。这意味着当您到达该语句时,您将离开while循环。所以无论如何,你的功能都会离开循环。一个好的迹象表明你的while循环不正确或不合适的是你最后打破一个休息时间。这是一个更好的方法。

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")

循环继续无限期地发生了什么。输入正确的输入后,它会从循环中断开。如果输入不正确,它只会再次循环。

即使你没有提出这个问题,我也会对另一半发表评论。首先,你的情况没有意义。所有数字都超过20或低于99.您需要使用and,以便两者必须为真。但是,另一部分是你甚至不需要这个条件语句。我们已经知道我们处于这个极限。这是我们在之前的while循环中确定的内容。最后,如前所述,while本身并不需要。如果您想使用只使用一次的条件,只需使用if语句即可。虽然是循环的意思,但如果你只使用一次,则强制你在最后有一个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")

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`