Break语句只对登录的一半有效

2024-07-04 05:17:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码末尾有一个break语句有问题。如果我有它,然后当用户输入错误的用户名和/或密码的程序将退出,而不是继续while循环。如果没有,那么在用户成功登录之后,当用户应该退出程序时,系统会提示用户再次输入用户名。我应该做些什么改变才能让一切顺利

我尝试过输入和输出break语句,以及输入while循环

#login
count = 0
if welcome == "y":
    while True:
        while count < 3:
            use = input("Username:")
            pwd = input("Password:")
            found_username = False
            with open("Credentials.txt", "r") as credentials_file:
                for line in credentials_file:
                    username_login, password_login = line.strip().split(':')

                    if use == username_login:
                        found_username = True
                        if pwd == password_login:
                            print("welcome you are now logged in ")
                            break
                        else:
                            print("Password is incorrect!")
                            count += 1
                            if count == 3:
                                print("Attempts exceeded")
                        break

                if not found_username:
                    print("Username and or password do not exist!")
                    count += 1
                    if count == 3:
                        print("Attempts exceeded")
                else:
                    break

        break

你知道吗\\\\ 当用户成功登录时,程序应该退出。如果用户输入了错误的用户名和/或密码,则应提示用户再次输入用户名和密码,直到最多正确输入3次


Tags: 用户程序密码ifcount错误usernamelogin
2条回答

break语句是无条件执行的,因此您永远不会重复循环

它应该只在found_username为真时执行,所以将它放在最后ifelse:块中

            if not found_username:
                print("Username and or password do not exist!")
                count += 1
                if count == 3:
                    print("Attempts exceeded")

            else:
                break

考虑到当前构建代码的方式,实际上需要两个循环,一个用于监视尝试次数,另一个用于检查有效凭据。也就是说,我强烈建议将这两个循环解耦,以便于您对问题进行推理。这就是为什么分解代码如此重要的原因—它使您能够更容易地发现错误

此外,还可以利用一些附加的Python概念(1) 在这里引发异常以抛出特定错误(例如用户名不存在而密码不正确),(2)分解continuebreak之间的差异continue用于跳转到循环的下一个迭代break用于完全脱离循环

对于您的代码,我建议如下:

def authenticate(username, password):
    with open("Credentials.txt", "r") as credentials_file:
        for line in credentials_file:
            username_login, password_login = line.strip().split(':')
            if username_login == username and password_login == password:
                return True  # Everything matches!
            elif username_login == username and password_login != password:
                raise Exception('Password is incorrect!')
            else:
                continue  # Skip to next line in credentials file

    raise Exception('Username does not exist')


if welcome == 'y':
    is_authenticated = False
    for i in range(3):
        username = input('Username:')
        password = input('Password:')

        try:
            authenticate(username, password)
            is_authenticated = True
            break  # Don't need to provide another attempt
        except Exception as e:  
            print(e)  # Print error

    if is_authenticated:
        print('Welcome you are now logged in')
    else:
        print('Attempts exceeded!')

相关问题 更多 >

    热门问题