Login program only reading last line of text file

104 Views Asked by At

As a continuation of my previous question Creating a login program using a text file, my program is trying to validate the username and password that a user enters and if the username and password exists in the user.txt, the user should be able to access the menu. The problem is that it only reads the last line of the user.txt file so only the last registered user is allowed to login. I want all users in the user.txt file to be able to login.

This is what I've tried so far

usernames = []
passwords = []

user_name = input("Please enter your username: ")
pass_word = input("Please enter your password: ")

with open("user.txt", "r+") as f1, open("tasks.txt", "r+") as f2:

    for lines in f1:
        logins = lines.strip()
        logins = lines.split(", ")
        username = logins[0]
        password = logins[1]
        usernames.append(logins[0])
        passwords.append(logins[1])

    while user_name == username and pass_word == password:
        menu = input('''Please select one of the following options:\n
        r - register a user 
        a - add task
        va - view all tasks
        vm - view my tasks
        e - exit ''').lower()

Contents of user.txt file

admin, adm1n
Maverick, FlyGuy
4

There are 4 best solutions below

1
BoppreH On

You're not using the usernames and passwords lists, so the check is done against only the last iteration/line. It's also easier to verify credentials if the (username, password) are stored as pairs, like so:

username_and_passwords = []

entered_username = input("Please enter your username: ")
entered_password = input("Please enter your password: ")

with open("user.txt", "r+") as f1, open("tasks.txt", "r+") as f2:

    for lines in f1:
        username, password = lines.strip().split(", ")
        username_and_passwords.append((username, password))

    if (entered_username, entered_password) not in username_and_passwords:
        print('Invalid username or password')

    while True:
        menu = input('''Please select one of the following options:\n
        r - register a user 
        a - add task
        va - view all tasks
        vm - view my tasks
        e - exit ''').lower()

You can also store the credentials in a dictionary that has username as keys and passwords as values. That would make it easier to check if the username is correct, but the password isn't.


I'm assuming this is a homework question, since anyone running the program can also read the user.txt file and get all the passwords. A more secure solution would hash the passwords and compare them using a constant-time algorithm.

2
bassam malla On

try this code

users = {}

user_name = input("Please enter your username: ")
pass_word = input("Please enter your password: ")

with open("user.txt", "r+") as f1:

for lines in f1:
    logins = lines.strip()
    logins = lines.split(", ")
    username = logins[0]
    password = logins[1].replace('\n','')
    users[username] = password
    # passwords.append()
# if user_name in users:
while user_name in users and users[user_name] == pass_word :
    menu = input('''Please select one of the following options:\n
    r - register a user 
    a - add task
    va - view all tasks
    vm - view my tasks
    e - exit ''').lower()
0
Henri On

Your issue is that you're using username and password to compare against what the user enters here

while user_name == username and pass_word == password: username and password will always be equal to the last entry in the text file because of your for loop:

 username = logins[0]
 password = logins[1]

The next issue is your logic for trying to match the correct username to password. One way to do it would be using a dictionary.

First use a similar method to this to add your username and passwords:

users = dict()
//inside your loop
  users[username]=password

Next to check if the user name matches the password use a similar approach:

if user_name in users.keys() and users[user_name]==pass_word:
//your other code
1
MohammadJavad Yousefi On

There is nothing wrong with reading user.txt. The problem is that you store the last line of this file to the username and password variables, and you use these variables to log in. Instead, you need to use the list of logins or different data structures such as tuple or dictionary.

For example:

usernames = []
passwords = []

user_name = input("Please enter your username: ")
pass_word = input("Please enter your password: ")

with open("user.txt", "r+") as f1, open("tasks.txt", "r+") as f2:

    credential = {}
    for lines in f1:
        logins = lines.strip().split(", ")
        username = logins[0]
        password = logins[1]
        credential[username] = password

    if user_name in credential.keys():
        if pass_word == credential[user_name]:
          menu = input('''Please select one of the following options:\n
          r - register a user 
          a - add task
          va - view all tasks
          vm - view my tasks
          e - exit ''').lower()