繁体   English   中英

尝试将 1 添加到变量并打印它,但它输出相同的数字

[英]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')

我尝试打印“number_of_trys”,但它一直输出“1”。 是的,我试过 number_of_trys = number_of_trys + 1。

每次你在 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.

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