Home > Blockchain >  Only the user can enter the program, but other users cant enter the program
Only the user can enter the program, but other users cant enter the program

Time:04-21

I have made changes to my code so that the program runs through the whole file containing the usernames and passwords for the program, but I get feedback that only the username 'admin' can have access to the program, and not other users.

Is there another way where the program can maybe run through the whole file and see if the username entered is the same as the username in the file?

My code is as follows:

def login():

username = input("Enter your username: ")
password = input("Enter your password: ")

for line in open('user.txt', 'r').readlines():
    field = line.strip().split(", ")
    if username == field[0] and password == field[1]:
        print('Welcome'   username)
        return True, field[0] == "admin"

    return False, False

login_success, is_admin = login()

if login_success and is_admin: display_admin_menu_options() elif login_success: display_menu_options() else: print("Username or password incorrect!")

CodePudding user response:

Your outer return statement is inside the for-loop. Try:

def login():
    username = input("Enter your username: ")
    password = input("Enter your password: ")

    for line in open('user.txt', 'r').readlines():
        field = line.strip().split(", ")
        if username == field[0] and password == field[1]:
            print('Welcome'   username)
            return True, field[0] == "admin"

    return False, False

My guess is that admin is at the top of user.txt file. So as soon as you input the admin password and username, it matches the fields and you return True,True. But if you input something else, it skips the if statement and returns False,False. You never loop more than once because your return statement always terminates the program in the first loop.

  • Related