Login doesn't work in my python project, any answers?

Viewed 29

When I register 2 accounts and then login in with the account on the first line in 'account.txt'. It doesn't work but it works for the second one. Can anyone tell me why? And if yes can you fix it, I'm new to python, Thanks!

In the login function, I made it so when you enter your username and password, they are stored in user0 and pass0. Then it checks if they equal to the first section of each lines and if it does then if will log you in.


Here's the code:

import csv


#======================================================================#
def register():

    firstname = input("Enter your first name: ")
    print("First name set to: " + firstname)
    surname = input("Enter your surname: ")
    print("Surname set to: " + surname)
    age = int(input("Enter your age: "))
    print("Age set to: " + str(age))
    password = input("Enter a strong password: ")
    print("Password is: " + password)
    
    if len(password) < 6:
        print("Password too weak.")
        password = input("Enter a strong password: ")

    username = firstname[:1] + surname + str(age)
    print("Your username is: " + username)

    with open("account.txt", "a", newline = "") as myFile:
        writer = csv.writer(myFile)
        writer.writerow([username.lower(), password, firstname.lower(), surname.lower(), age])
        myFile.close()
        print("Account successfully made! Please login now.")
        loginMenu()
#======================================================================#

#======================================================================#
def databaseOptionMenu():
    
    print("#========================================#")
    print("|   Please select what you want to do.   |")
    print("|                     |")
    print("|         Press 1 to Load table          |")
    print("|         Press 2 to Add Student         |")
    print("|        Press 3 to Remove Student       |")
    print("|        Press 4 to Search Student       |")
    print("|                     |")
    print("|     Press 5 to see account details     |")
    print("|           Press 6 to Log out           |")
    print("#========================================#")
    
    try:
        selection1 = int(input("Option: "))
    except:
        print("Select from 1 to 6 please.")
        databaseOptionMenu()
        
    if selection1 == 6:
        loginMenu()
    elif selection1 == 5:
        with open("account.txt", "r") as myFile:
            accountInfo = myFile.read().replace("\n", "")
            print(accountInfo)
#======================================================================#

#======================================================================#
def login():
    
    user0 = input("Username: ")
    pass0 = input("Password: ")
    
    try:
        file = open("account.txt", "r")
    except:
        print("Account doesn't exist! Please register an account.")
        loginMenu()

    for line in file:
        details = line.split(",")

    if user0 == details[0] and pass0 == details[1]:
        print("Successfully logged in!")
        databaseOptionMenu()
    else:
        print("Incorrect Username or Password, please try again.")
        loginMenu()
#======================================================================#

#======================================================================#
def loginMenu():
    
    print("#===================================#")
    print("|    Welcome to Python Database!    |")
    print("|                    |")
    print("|        Press 1 to Register        |")
    print("|         Press 2 to Login       |")
    print("#===================================#\n")
    
    try:
        selection = int(input("Option: "))
    except:
        print("Select from 1 to 2 please.")
        loginMenu()
    
    if selection == 1:
        register()
    elif selection == 2:
        login()
 #======================================================================#
        
loginMenu()
        
1 Answers

In your login function, the details variable will always contain data from the last file row, since for each line you overwrite the previous value with the data from the current row. Try adding each row data to a list, and then compare the login data with each list item, like this for example:

def login():

    user0 = input("Username: ")
    pass0 = input("Password: ")

    try:
        file = open("account.txt", "r")
    except:
        print("Account doesn't exist! Please register an account.")
        loginMenu()


    detailsList = []
    for line in file:
        detailsList.append(line.split(","))

    loggedIn = False

    for details in detailsList:
        if user0 == details[0] and pass0 == details[1]:
            loggedIn = True
            break

    if loggedIn:
        print("Successfully logged in!")
        databaseOptionMenu()
    else:
        print("Incorrect Username or Password, please try again.")
        loginMenu()
Related