fcorby
fcorby

Reputation: 1

Why Is My Login System Not Working - Python?

I'm working on a login system for one of my school projects and I have linked the code to a CSV file that when the user registers it'll save their name and password. However when you try to re-login the result is that the name or password is incorrect, but when I say I don't have an account and make one it works.

import csv
import random

# create a list to store the usernames and passwords of both players
players = []

# loop through each player
for i in range(2):

    # ask if the player has made a username and password before
    newUser = input(f"Player {i+1}, have you made a username and password before?").strip()

    # if the player is new, ask for their name and password and add them to the list
    if newUser == 'no':
        newName = input(f"Player {i+1}, what is your first name?").strip()
        newPassword = input(f"Player {i+1}, what would you like your password to be?").strip()

        players.append([newName, newPassword])

        file = open('ScoreSheet.csv', 'a', newline='')
        writer = csv.writer(file)

        writer.writerow([newName, newPassword])

        file.close()

    # if the player has logged in before, ask for their name and password and check if they match
    elif newUser == 'yes':
        verifyName = input(f"Player {i+1}, what is your name?").strip()
        verifyPassword = input(f"Player {i+1}, what is your password?").strip()

        # loop through each player's login information and check if there is a match
        match_found = False
        for player in players:
            if player == [verifyName, verifyPassword]:
                print(f"Player {i+1}, you are logged in")
                match_found = True
                break

        # if there is no match, inform the player and ask them to try again
        if not match_found:
            print(f"Player {i+1}, wrong username or password. Please try again.")

# print welcome message with the players' names
if len(players) >= 2:
    print(f"Welcome, {players[0][0]} and {players[1][0]}!")
else:
    print("Error: not enough players logged in.")

When I print the CSV file and what it contains it shows all of the names that have been previously registered:


'finley'  'fin'  '10'
janet janet123 None
james james123 None
charlie charlie123 None
amy amy123 None
fred fred123 None
summer summer123 None
nat nat123 None
nathan nathan123 None
elle elle123 None
charles charles123 None
juno juno123 None
sam sam123 None
samuel samuel123 None
Tom Tom123 None
Thomas Thomas123 None
charlie charlie123 None
grace grace123 None
natalie natalie123 None
Dan Dan123 None
Daniel Daniel123 None

I am new to CSV so I used a google sheet to create the file but then it got opened in VS Code which only shows 2 names:

finley,fin,10
janet,janet123

It's formatted like: name,password,score I'm also using PyScripter for the actual code

Upvotes: 0

Views: 78

Answers (1)

When I read your code it seems like you are not really reading the .CSV file but only from the Playerslist. i would try add this code before checking for login credentials. it uses the òpen function that reads the .CSV file and then we store that data in the players list

#open the CSV file and read the data into the players list
with open('ScoreSheet.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        players.append(row)

Upvotes: 0

Related Questions