def checked(input, correct):
if input == correct:
return True
else:
return False
while True:
try:
user_typed = str(raw_input('Type password\n'))
password = str('test')
number_of_trys = 0
if checked(user_typed, password) is True and number_of_trys <= 3:
print('Welcome User')
break
else:
print("Password is incorrect, please try again.")
number_of_trys += 1
print(number_of_trys)
finally:
print('Loop Complete')
I try to print "number_of_trys" but it keeps outputting '1'. And yes, I have tried doing number_of_trys = number_of_trys + 1.
Every time you're asking for the password in try:, you're setting number_of_trys to 0. Declare it outside of the while loop and reset it in the if statement
def checked(input, correct):
if input == correct:
return True
else:
return False
number_of_trys = 0
while True:
try:
user_typed = str(raw_input('Type password\n'))
password = str('test')
if checked(user_typed, password) is True and number_of_trys <= 3:
print('Welcome User')
number_of_trys = 0
break
else:
print("Password is incorrect, please try again.")
number_of_trys += 1
print(number_of_trys)
finally:
print('Loop Complete')
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.