【问题标题】:While True looping the results of a 'if elif' statementWhile True 循环“if elif”语句的结果
【发布时间】:2021-04-15 17:46:34
【问题描述】:

我正在尝试使用 While 循环,但遇到了这个问题:

代码:

x = int(input('Guess the number 1-10'))

while True:
    if (x == 8):
        print('yay!')
        break
    else:
        print('No No')

结果:

No No
No No
No No
No No
No No
No No
No No
No No

直到我停止它...

有些人建议使用break,但我不想在他们出错时停止它,我希望它进行多次尝试,直到他们得到正确的数字。我能做什么?

【问题讨论】:

    标签: python-3.x while-loop


    【解决方案1】:

    您想在每次迭代中询问用户输入:

    while True:
        x = int(input('Guess the number 1-10'))
    
        if (x == 8):
            print('yay!')
            break
        else:
            print('No No')
    

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 2021-10-31
      • 2015-05-19
      • 1970-01-01
      • 2020-08-08
      • 2013-06-09
      • 2014-01-21
      • 2015-11-08
      • 2016-01-12
      相关资源
      最近更新 更多