2016-09-19 99 views
-1

我是一个初学者蟒蛇学习者和我写了下面的代码要求用户输入帐户销:如何格式化while循环语法?

while True: 
    print ('Please enter your pin.') 
    pin = input() 
    if pin =='2356': 
     print('Access granted') 
     break 

现在......如果我只希望允许用户输入不正确的PIN 5倍在“冻结账户”之前,我该怎么做?我甚至使用正确的循环类型?

谢谢!

回答

0

是的,你几乎在那里。您可以通过递增如下数值来计算用户输入不正确引脚的次数:

incorrect_tries = 0 
while incorrect_tries <= 5: 
    print ('Please enter your pin.') 
    pin = input() 
    if pin =='2356': 
     print('Access granted') 
     break 
    else 
     incorrect_tries +=1