Sam Sam - 11 months ago 87
Python Question

Saving user details into a file in a login system?

I am producing a turn based game for a project and would like to create a user login system that saves the user's login username and password into a file in the game folder location. I would hope to then use the notepad file to check if there is already an account with that username when new users try to create a login.

In this code i attempted this but it is wrong?!

filename = 'UserInfo.txt'
openfile = open(filename, "r")
UserData = openfile.readline()
def displayMenu():
print (UserData)
status = input("Are you a registered user? y/n? ")
if status == "y":
elif status == "n":

def newUser():
createLogin = input("Create login name: ")

if createLogin in UserData:
print ("Login name already exist!")
createPassw = input("Create password: ")
UserData[createLogin] = open('UserInfo','w')
print("User created!")
def oldUser():
login = input("Enter login name: ")
passw = input("Enter password: ")
if login in UserData and UserData[login] == passw:
print ("Login successful!")
print ("User doesn't exist or wrong password")

Answer Source

You should really be putting the error message up but I think I have spotted what is wrong. In newUser() you are opening 'UserInfo' not 'UserInfo.txt' also open in a mode not w mode. So write open('UserInfo.txt', 'a')

You also need to read all the lines not just one line openfile.readlines() Also remember to close the file after you open it in read mode or else it wont let you write to it:

openfile = open(filename, "r")   
UserData = openfile.readline()