首页 > 解决方案 > 登录总是说不正确

问题描述

我的代码总是说它是不正确的,即使它出现在文本文件中也很困难,它以前可以工作,但现在由于某种原因不能工作。

def select_login_signup():
    while True:
        selection = input("Welcome to sports.com, please select"
                      " \"L\" to Log In with your account or \"S\" to create an account: ")
        if selection.lower() == 's':
            register()
            answer = input("Would you like to Log In? Y/N? ")
            while not answer:
                if answer.lower() == "y":
                    login()
                    break
                elif answer.lower() == "n":
                    exit()

                else:
                    answer = False
                    print("Invalid answer.")
                    continue
        elif selection.lower() == 'l':
            login()
            break
        else:
            print("Invalid answer.")
            continue


def register():
    username = input("Create your username (no more than 10 characters or less than 4.): ")
    while 10 < len(username) < 4:
        print('username cannot have more than 10 characters or less than 4.')
        username = input("Create your username (no more than 10 characters or less than 4.): 
")
        break
    while username.isnumeric():
        print("username must contain at least one letter")
        username = input("Create your username (no more than 10 characters or less than 4.): 
")
        break

    password = input("Create a password with letters and numbers: ")
    while len(password) < 6:
        print("Your password must contain more than 6 characters.")
        password = input("Create a password with letters and numbers: ")
        continue
    while password.isnumeric() or password.isalpha():
        print("Your password must contain both letters and numbers")
        password = input("Create a password with letters and numbers: ")
        continue

    login_credentials = open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', "a")
    login_credentials.write(f'\n{username},{password}')
    login_credentials.close()

    print("Account created successfully.")


def login() -> object:
        username = input("Please enter your username: ")
        username = username.strip()
        password = input("Please enter your password: ")
        password = password.strip()
        login_credentials = open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', "r")
        login_credentials.readlines()
        with open('C:\\Users\\hmarq\\Documents\\UsernameAndPassword.txt', 'r') as 
login_credentials:
            for line in login_credentials:
                login_info = line.split(",")
                if username == login_info[0] and password == login_info[1]:
                    print("Authorized")
                    authenticated = True
                    return authenticated
                else:
                    print("Incorrect credentials.")
                    username = input("Please enter your username: ")
                    username = username.strip()
                    password = input("Please enter your password: ")
                    password = password.strip()
                    continue


def main():
    select_login_signup()


if __name__ == "__main__":
    main()

标签: python

解决方案


使用 open('C:\Users\hmarq\Documents\UsernameAndPassword.txt', 'r') 作为 login_credentials

当你打开一个文件时,你必须格式化数据,因此数据是不一样的。

如果用户名 == login_info[0] 和密码 == login_info[1]:

我希望它对你有用,问候。


推荐阅读