简体   繁体   中英

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?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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