Моя система входа не работаетОн принимает только один аккаунт.Как я могу заставить его работать? - PullRequest
0 голосов
/ 04 декабря 2018

Это мой код:

if login_system == "1":

        user = open("userAccount.txt","r")

        while not loggedIn:

            Username = input("Enter player 1 username")
            Password = input("Enter player 1 password")

            for row in user:
                #print("ROW:", row)
                field = row.split(",")
                ID = field[0]
                username = field[1]
                password = field[2]
                score = field[3]
                lastchar = len(score)- 1
                score = score[0:lastchar]

                if Username == username and Password == password:
                    print("Welcome to dice game" + Username)
                    x = 0
                    loggedIn = True
                    break   
            print("Not the correct username and password. Try again or create a new account")

        user.close()

    elif login_system == "2":
        x = 0

    else:
        print("INVALID! Please try again.")

Вот что он выдает:

Welcome to the dice game
Press 1 if you want to sign in or press 2 if you want to sign up1
Enter player 1 usernameabid
Enter player 1 passwordpassword1
Welcome to dice gameabid
Not the correct username and password. Try again or create a new account
Round 1
Player 1 score: 0
Player 2 score: 4
Player 2 wins
Round 2
Player 1 score: 16
Player 2 score: 18
Player 2 wins
Round 3
Player 1 score: 16
Player 2 score: 2
Player 1 wins
Round 4
Player 1 score: 23
Player 2 score: 18
Player 1 wins
Round 5
Player 1 score: 2
Player 2 score: 0
Player 1 wins
The final score
player 1 highscore is: 57
Player 2 highscore is: 42
Player 1 wins

Welcome to the dice game
Press 1 if you want to sign in or press 2 if you want to sign up1
Enter player 1 usernameadam
Enter player 1 passwordpassword2
Welcome to dice gameadam
Not the correct username and password. Try again or create a new account

Я хочу обе учетные записииз базы данных на работу.Как я использую любую учетную запись из базы данных для входа в систему. Спасибо за помощь.

Новый код:

while x == 1:
        login_system = input("Press 1 if you want to sign in or press 2 if you want to sign up")

        if login_system == "1":

            user = open("userAccount.txt","r")

            while not loggedIn:

                Username = input("Enter player 1 username")
                Password = input("Enter player 1 password")

                for row in user:
                    #print("ROW:", row)
                    field = row.split(",")
                    ID = field[0]
                    username = field[1]
                    password = field[2]
                    score = field[3]
                    lastchar = len(score)- 1
                    score = score[0:lastchar]

                    if Username == username and Password == password:
                        print("Welcome to dice game" + Username)
                        x = 0
                        loggedIn = True
                        break   
                #print("Not the correct username and password. Try again or create a new account")

            user.close()

        elif login_system == "2":
            x = 0

        else:
            print("INVALID! Please try again.")

Код работает, но если я несколько раз ввожу неправильное имя пользователя и пароль, а затем введите правильное имя пользователя и пароль, он не будет работать.

Welcome to the dice game
Press 1 if you want to sign in or press 2 if you want to sign up1
Enter player 1 usernamesdas
Enter player 1 passworddvgdvg
Enter player 1 usernamevgfrgr
Enter player 1 passwordrggr
Enter player 1 usernameabid
Enter player 1 passwordpassword1
Enter player 1 usernameadam
Enter player 1 passwordpassword2
Enter player 1 username
Enter player 1 password
Enter player 1 username
...