[英]Try to add 1 to a variable and print it, but it outputs the same number
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'.我尝试打印“number_of_trys”,但它一直输出“1”。 And yes, I have tried doing number_of_trys = number_of_trys + 1.
是的,我试过 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每次你在 try: 中询问密码时,你都将 number_of_trys 设置为 0。在 while 循环之外声明它并在 if 语句中重置它
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.