Goh Yu Heng
Goh Yu Heng

Reputation: 15

How do I match an user input to username in my txt file in Python?

from time import sleep
import time

users = {}
status = ""
        
def loading():
    spaces = 0                                      
    while spaces < 3:
        print("Loading" + "." + " " + "." + " " + "." + " " + "." + " " )
        spaces = spaces + 1
        sleep(1)
        
def mainMenu():
    global status
    status = input("Do you have a login account? y/n Or press q to quit\n")
    if status == "y":
        oldUser()
    elif status == "n":
        newUser()

    elif status == "q":
        quit()

#Creating function for new user account creation
def newUser():
    createUser = input("Please create a username: ")
    for line in open(r"C:\Users\User\Desktop\Python Project\accountdetails.txt", "r").readlines():
        user_info = line.split()
        createdUser = user_info[0]
    if createUser in createdUser:
        print("\nUsername already exist!\n")
    else:
        createPass = input("Please type a new password: ")
        users[createUser] = createPass
        print("\nAccount Created\n")
        login = open(r"C:\Users\User\Desktop\Python Project\accountdetails.txt", "a")
        login.write("\n" + createUser + " " + createPass + "\n")
        login.close()

#Creating function for old account login
def oldUser():
    userInput = input("Enter username: ")
    passwInput = input("Enter password: ")
    #To check if user and passw matches any existing
    for line in open(r"C:\Users\User\Desktop\Python Project\accountdetails.txt", "r").readlines():
        login_info = line.split()
        user = login_info[0]
        passw = login_info[1]
        users[user] = passw
    if userInput in users and users[userInput] == passwInput:
        loading()
        print("\nLogin Successful!\n")
        sleep(1)
        print(userInput, "accessed the system on:", time.asctime())

    else:
        print("\nUser doesn't exist or wrong password\n")
            
while status != "q":
    mainMenu()

    

I am trying to check if any inputted username match within a given text file. I had this under the newUser function, however when I use the 'in' operator it doesn't help at all. I have tried to print the result of createdUser which corresponds to:

max
nicky
tom
kai

However only the result which is kai was taken into account, and if I had input other names such as max, nicky or tom, it would not indicate that the username existed.

Upvotes: 0

Views: 270

Answers (1)

ivvija
ivvija

Reputation: 602

You overwrite the variable here: createdUser = user_info[0].
Only the last value is kept as a string.

Use a list and collect all names:

def newUser():
    createUser = input("Please create a username: ")
    createdUser = []
    for line in open(r"C:\Users\User\Desktop\Python Project\accountdetails.txt", "r").readlines():
        user_info = line.split()
        createdUser.append(user_info[0])

Edit: Added changes proposed by Klas, also changed variable name.

def newUser():
    createUser = input("Please create a username: ")
    with open(r"C:\Users\User\Desktop\Python Project\accountdetails.txt", "r") as f:
        usernameSet = {line.split()[0] for line in f}

Upvotes: 2

Related Questions