[Solved] Can someone suggest why the second player is not being authorised?


This is far too much code for this task. You need to better structure your coding – seperate it in task. You never read all the usernames and passwords.

What you need is to build a data strucure that holds user and the password, for that you need to read in both files, line by line and “connect” the lines.

If you need to check for login you just use that structure you build and look it up. The “Need to login code” parts do not need to handle file reading at all.

Create password and user file:

with open ("user.txt","w") as u, open("pass.txt","w") as p:
    for num in range(3):
        u.write(f"user{num+1}\n")
        p.write(f"pass{num+1}\n")

Read file in and store in dictionary:

# read all users/pw into a dictionary
users = {}
# open both files
with open("user.txt") as u, open("pass.txt") as p:
    # connect the lines using zip() with pairs the lines into tuples
    for (user,pw) in zip(u,p):
        # set the user/pw in your dictionary
        users[user.strip()] = pw.strip()

# don't print in your application     
print(users)

Output:

{'user1': 'pass1', 'user2': 'pass2', 'user3': 'pass3'}

Program using the dict:

With that out of hand you can easily check if a user can log in:

while True:
    user = input("Enter user name (empty user to quit): ")
    if not user:  
        break
    # never explain that this user is not present, ask for pw and
    # quite with a vague message that either user or pw wrong for
    # "more" security
    pw = input("Enter passphrase: ")

    # look if that key is in the dict and if so if the passphrase matches
    if users.get(user) == pw:
        print("Logged in.")
        break
    else:
        print("Invalid user.")
if not user:
    print("Quitting")
    exit(1)
else:
    print("Gooooooo")
    # logged in

5

solved Can someone suggest why the second player is not being authorised?