0

I have a code that writes usernames, passwords and scores into a csv file in the format: username, password, score. I have it so it writes the username and password at the beginning of the code:

def login_system():
loggedin = False
while not loggedin:
    login=input("Do you have an account? (yes/no) ")
    loggedin=False
    if login.lower()=="yes":
        login=open("login.csv")
        reader = csv.reader(login)
        username=input("What is your username: ")
        password=input("What is your password: ")
        for row in reader:
            if row[0]==username and row[1]==password:
                print("Welcome " + username)
                loggedin=True
                login=open("login.csv","a")
                login.write(username + "," + password + "\n")
                login.close()
                break
        if loggedin==False:
            print("Invalid username or password. Please try again.")
            continue

I do this for user one and user 2.

print("User 1:")
login_system()

print("")
print("User 2:")
login_system()

In the code, this puts the usernames and passwords into column 0 and 1.

Then at the end after the game, it writes the score into the csv file. The part i'm struggling with is then writing the score into column 2. I need it to write on the next available cell in the csv file in row 2.

Any help on this would be greatly appreciated.

Dylan

Dylan
  • 25
  • 3
  • why are you storing scores nect to user credentials? Wouldn't it be easier to create a seperate file that holds lines with `username, 10239453`? You could read that file into memory at start and simply overwrite the file with whatever is in memory when your game ends. I am also kindof astonished that your current code works - you are opening the same file twice, once for reading and while still open once for appending. Instead of checking the file line for line it would be better to read all usercreds into a (f.e.) dictionary and then compare if that user/pw is a key:value in that dict. – Patrick Artner Nov 05 '18 at 12:15
  • Because at the very end, i print the top five scores of all time by finding the top five scores in row 2 at the end so its just easier to have them in the same CSV file. I had it before but i simplified the code and now the old was i used it doesn't work. . I have it like this because it is simple. It writes the username and password for user 1, then for user 2 on a separate line. All I need is to write the score of user(x) next to the login details in the CSV. – Dylan Nov 05 '18 at 12:23
  • 1
    Possible duplicate of [How to add a new column to a CSV file?](https://stackoverflow.com/questions/11070527/how-to-add-a-new-column-to-a-csv-file) – Carlos Mermingas Nov 05 '18 at 12:37

2 Answers2

0

If I correctly interpreted your request, you simply have to modify this line:

login.write(username + "," + password + "\n")

with

login.write(username + "," + password + ",")

and then add the score with the line terminator '\n'. In this way you'll get:

username,password,score

all in the same line.

Woodstock94
  • 196
  • 10
0

I rewrote your code, so it's readable:

def login_system():
    loggedin = False
    with open("login.csv", newline='') as fid:
        reader = csv.reader(fid, delimiter=",")
        login = list(reader)
    while not loggedin:
        ans=input("Do you have an account? (yes/no) ")
        if ans.lower()=="yes":
            username=input("What is your username: ")
            password=input("What is your password: ")
            if [username, password] in login:
                print("Welcome " + username)
                loggedin=True
                return loggedin
            if loggedin==False:
                print("Invalid username or password. Please try again.")
                continue

        elif ans.lower() == "no":
            username=input("New username: ")
            password=input("New password: ")
            with open("login.csv", "a") as login:
                login.write(username+","+password+"\n")
            print("Welcome "+username)
            loggedin = True
            return loggedin
  • don't give two variables the same name
  • use the with statement for file handling
  • don't open and close the same file hundreds of times, when you only need to do so once

Lastly to your actual question:

It's way easier just writing a new file or overwriting the old one. And also faster.

user8408080
  • 2,428
  • 1
  • 10
  • 19