【问题标题】:My code isn't printing any of the while loop when I give it valid input. How do I fix?当我给它有效输入时,我的代码没有打印任何 while 循环。我该如何解决?
【发布时间】:2022-12-12 10:44:24
【问题描述】:

试图运行:

pin = int(input('Enter your PIN: '))

while pin != 1234:
  pin = int(input('Incorrect PIN. Enter your PIN again: '))
  
  if pin == 1234:
    print('PIN accepted!')

它只是让我在控制台中输入一个代码,但如果我输入 1234,则不会显示任何类型的输出响应。如果我输入错误的代码,它会运行同时销环形。我如何让它运行如果引脚声明?

我试图切换代码,但如果我这样做,我会遇到很多错误。尝试重新启动 visula studio,但这也没有用。我知道答案必须很简单,但我还不够了解,无法自己找到答案。

【问题讨论】:

    标签: python if-statement while-loop nested-loops


    【解决方案1】:

    if 语句移到 while 循环之外。

    pin = int(input('Enter your PIN: '))
    
    while pin != 1234:
      pin = int(input('Incorrect PIN. Enter your PIN again: '))
    
    if pin == 1234:
      print('PIN accepted!')
    

    问题是它只在 pin 时进入 while 循环不是1234.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 2016-01-03
      • 2021-07-06
      • 2019-08-26
      • 1970-01-01
      • 2019-08-21
      相关资源
      最近更新 更多