Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

python: my code doesn't loop into the for loop to read the csv

I am trying to create a code where two pre-existing users can login to an account, and it is authorized from a csv file - these players can then play a dice game. I have completed the game, however my code doesn't read my loop with the information for reading the csv in. I have tried many different ways, including simply reading the rows alone, splitting the lines, and many other things. My code doesn't return any errors, instead just runs the code, without authenticating the users. I will need the code to search for the username, if it exists check if the password in the box to the immediate right matches the password given from the code, print the user is logged in. If the user and password don't match, let the user enter the password a further two times, and if the password still doesn't match print they can't login.

Any help would be appreciated, thank you.

import csv #importing my csv with my authenticated users in
import random #importing random to use to generate a random number



def login1(): #creating a function so i can do the login cycle again
    loggedin1 = False #making user1 not logged in
    global username
    username = input("Please enter your username:")# ask the users username    
    password = input("Please enter your password:")#ask user for password
    with open("game.csv","r+")as csvfile: # opening my csvfile
        datareader = csv.reader(csvfile, delimiter=',') #reading my csv file
        for row in datareader:
            user = line.split(",")
            user = user[0]
            print(user) # to test if it is running this 
            if username == user:
                print("User name exists")
                for row in datareader:
                    passw = line.split(",")
                    passw = passw[0]
                    if password == passw:
                        print("hello",username,"you are logged in")
                    else:
                        password =input("Password is wrong, please try again")            
            elif userexists == False:
                print("no username found, please enter the username again")
                login1()
    return(username)
login()
question from:https://stackoverflow.com/questions/65934831/python-my-code-doesnt-loop-into-the-for-loop-to-read-the-csv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Always save your data in the form of list or a dictionary as they are easier to operate and are mutable. data=[username,password] or {'username':username,'password':password}

to read through the csv file:

def login():
    f=open('data.csv','r')
    reader=csv.reader(f)
    u=input('Username: ')
    p=input('Password: ')
    for row in reader:
        try:
            if row[0]==u and row[1]==p:
                print('SUCCESS')
                break
        except EOFError:
            print('FAILED')
            break

Whenever the user failed to login call the function again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...