如何使这段代码再次启动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
答案 0 :(得分:0)
这称为异步编程。它已在Python中使用async和await关键字引入。
import asyncio
async def allowInput():
await asyncio.sleep(30000) #ms
# your code goes here
答案 1 :(得分:0)
您只需要将那条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
执行循环。