【问题标题】:Stopping a while statement停止 while 语句
【发布时间】:2015-09-16 22:13:00
【问题描述】:

在我的计算机科学入门课程中,我被分配编写一个程序,该程序将打印出要求用户输入的数字 (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 语句来测试输入的变量是否在 20 到 99 之间而不是循环。
  • 您有一个带有无条件break 的循环。这基本上只是一个if 声明。如果你真的想继续要求一个新号码,你需要在循环中这样做。看来您需要查看教科书中有关循环和输入检查的部分。

标签: python if-statement while-loop


【解决方案1】:

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

while a>20 and a<99:

也使用if,因为如果你不这样做,它会无限循环

【讨论】:

    【解决方案2】:

    您将“while”与“if”混淆了。你需要一个“if”语句; "while" 表示您打算重复的内容。

    另外,请注意 a>20 或 a总是为真;任何数字都是其中之一。我相信你在这里想要一个“and”,这使得这只是“if”语句的“else”子句。

    最后,我不确定您要如何处理您的第一笔印刷对帐单中的“end=”。这是语法错误。

    【讨论】:

    • end= 不是 python3 中的语法错误。当您有 end=" " 并使用 print 时,它会在末尾放置一个空格而不是换行符
    • 就是这样!谢谢!但是“end=”的原因是数字的第一部分和数字的第二部分在一行上,而不是两个。
    • 谢谢;我们仍然使用 Python 2.7。
    【解决方案3】:

    您已将 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 语句。 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")
    
    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`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 2014-07-13
      • 1970-01-01
      • 2014-07-01
      • 2022-09-29
      • 2015-05-07
      相关资源
      最近更新 更多