簡體   English   中英

如何在計時器后重新開始循環?

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

如何使這段代碼重新啟動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

這稱為異步編程。 它已在Python中使用async和await關鍵字引入。

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

您只需要將其移動, if counter == 3行並將其下面的塊移動到while循環中。

為了改善用戶看到的消息流,我還對代碼進行了一些重構。

這是一個例子:

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

該代碼將繼續循環,直到用戶輸入正確的密碼為止,此時它將break循環的執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM