Home > Blockchain >  login menu is appearing twice
login menu is appearing twice

Time:11-12

I am creating a simple program that checks a text file for usernames and passwords. if the username and password is found a message is printed and access to granted. it returns the success message if the details are correct however when i run my code i am prompted to enter my username and password twice.

this is the code

def login():
    loginUsername = input("Enter Username: ")
    loginPassword = input("Enter PASSWORD: ")

    with open('password.txt') as data:
        accounts = data.readlines()

    userfound = False
   
    for line in accounts:
        splitline = line.split("\t")
        
        if loginUsername == splitline[0] and loginPassword == splitline[1]:
            userfound = True
            break 
           
        
    return userfound

options = input("please make a choice 1 to register a new user account, 2 to login or 3 to exit to program \n")

user_found = login()

if options == "1":
    register()
elif options == "2":
    login()
elif options == "3":
    sys.exit()
else:
    print("Please make a valid choice")

 
if user_found:        
    print("LOGGED IN")
    # print menu 
else:
    print("Login FAILED")

this is what happens when the program runs. in this case incorrect details were run

please make a choice 1 to register a new user account, 2 to login or 3 to exit to program 2 Enter Username: 22 Enter PASSWORD: ww Enter Username: ww Enter PASSWORD: ww Login FAILED

CodePudding user response:

login is called twice. On user_found = login() and

elif options == "2":
    login()

You should probably only include it in the if-else statement.

if options == "1":
    register()
elif options == "2":
    user_found = login()
elif options == "3":
    sys.exit()
else:
    print("Please make a valid choice")

CodePudding user response:

The prompt to enter username and password is displayed twice because login() is called once irrespective of the user choice in the below line.

user_found = login()

The same login() is called for the second time when the user enters the choice 2.

elif options == "2":
    login()

If i understand your requirement correctly, The code needs to be modified like below.

options = input("please make a choice 1 to register a new user account, 2 to login or 3 to exit to program \n")


if options == "1":
    register()
elif options == "2":
    user_found = login()
elif options == "3":
    sys.exit()
else:
    print("Please make a valid choice")

 
if user_found:        
    print("LOGGED IN")
    # print menu 
else:
    print("Login FAILED")
  • Related