The Student Room Group

Python login/sign in

Hi, I'm new in Python programmation and I'm basically trying to able a user to create his login details (password + username) so that he is then able to log in by entering his details.
I have decided to use a csv file to store the user's data and I'm able to write in the file but after the user has enterred his details I'm not able to create a program to check if the username and password are correct and present in the file, and if yes, to log in, or in the opposite, see that the details are not present in any row and therefore to refuse the access to the user.

Here's my code for the moment:

UserData = open('UserInfo.csv', "r+")
writefile = open('UserInfo.csv', "r+")
import csv

def newUser():
createLogin = input("Create login name: ")
if createLogin in UserData:
print ("Login name already exist!")
else:
createPassw = input("Create password: ")
writefile.write(createPassw + "\n")
writefile.write(createLogin + "\n")
writefile.close()
print("User created!")

def oldUser():
with open('UserInfo.csv') as csvfile:
database = csv.DictReader(csvfile)
loggein = False
while loggein is not True:
username = input("Enter your're username:")
password = input("Enter you're password:")
for row in database:
Username_File = row['username']
Password_File = row['password']
if (Username_File == username and Password_File == password):
loggein = True
print(row['username'], row['password'])
print('Succesfully logged in!')
#loginmenu() add this later on

def displayMenu():
while 0 ==0:
print (UserData)
status = input("Are you a registered user? y/n? ")
if status == "y":
oldUser()
elif status == "n":
newUser()
else:
print("Sorry, invalid input. Please try again")
displayMenu()

Quick Reply

Latest