简体   繁体   English

如何在计时器后重新开始循环?

[英]How can I make my loop start over after a timer?

How can I make this code to start the while loop again until the user will put the right password? 如何使这段代码重新启动while循环,直到用户输入正确的密码为止?

userPassword =input('parola;')
userPasswordId = input('parola')
counter = 0
while userPasswordId != userPassword and counter < 3:
    print('Sorry the password is incorect.Try again!')
    counter = counter + 1
    print('You have', 3 - counter, 'attempts left.')
 userPasswordId = input('Enter your password:')
if counter == 3:
    print('Your account is locked for 30 seconds!!!!!')
    import time
    sec = 0
    while sec != 5:
        print('>>>>>>>>>>>>>>>>>>>>>', sec)
    # Sleep for a sec
        time.sleep(1)
    # Increment the minute total
        sec += 1

It's called asynchronous programing. 这称为异步编程。 It has been introduce in Python with async and await keyword. 它已在Python中使用async和await关键字引入。

import asyncio 
async def allowInput():
    await asyncio.sleep(30000) #ms
    # your code goes here

You just need to move that if counter == 3 line and the block below it into the while loop. 您只需要将其移动, if counter == 3行并将其下面的块移动到while循环中。

To improve the flow of messages that the user sees, I've refactored the code a bit as well. 为了改善用户看到的消息流,我还对代码进行了一些重构。

Here's an example: 这是一个例子:

import time


userPassword =input('parola;')
counter = 0

while True:
    userPasswordId = input('Enter your password:')
    if userPasswordId != userPassword:
        print('Sorry the password is incorect.Try again!')
        counter += 1
        print('You have', 3 - counter, 'attempts left.')
    else:
        break

    if counter == 3:
        counter = 0
        print('Your account is locked for 30 seconds!!!!!')
        sec = 0
        while sec != 5:
            print('>>>>>>>>>>>>>>>>>>>>>', sec)
        # Sleep for a sec
            time.sleep(1)
        # Increment the minute total
            sec += 1

This code will continue to loop until the user enters the correct password, at which point it will break execution of the loop. 该代码将继续循环,直到用户输入正确的密码为止,此时它将break循环的执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM