首页 > 解决方案 > 在循环中获取输入有什么区别?

问题描述

我在 python 中运行代码,想知道在循环中获取输入有什么区别。

例如;

restart = ('y')
chances = 3
balance = 1000
while chances >= 0 :
 pin = int(input('Please enter your 4 digit pin \n'))   
 if pin == (1234):
    print('You have entered the correct pin')
    while restart not in ('N', 'n'):
        print ('Press 1 to check balance')
        print ('Press 2 to withdraw')
        option = int(input('Can you please enter your option \n'))
        if option == 1:
            print('Your balance is', balance, '\n')
            restart = input('Do you like to go back?')
            if restart in ('n', 'N'):
                print ('Thanks')
                break

在上面的代码中,当我 pin = int(input('Please enter your 4 digit pin \n')) 在 while 循环之外时 if restart in ('n', 'N'):,这个 if 语句不起作用。它开始打印 print('You have entered the correct pin')而没有终止。

有人可以解释为什么吗?

标签: pythonwhile-loop

解决方案


“break”让你脱离了内部的 while 循环while restart not in ('N', 'n'):

由于“重新启动”的值将是 'n' \ 'N',因此您将永远不会再进入此循环,但是外部循环while chances >= 0 :会继续进行,因为您从未更改过机会的值。


推荐阅读