python read/write to file login script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



python read/write to file login script



I was wondering if anyone would help. I am new to python. I am trying to create a basic login script for a game, that will write a username and password to a text file. When logging in, it will read from that text file and compare the entry made by the user. The code is below:


def Register():
print("Hello! You need to register an account before you can begin")
username = input("Please enter a username: ")
password = input("Now please enter a password: ")
file = open("Login.txt","a")
file.write (username)
file.write (",")
file.write (password)
file.write("n")
file.close()
print ("Your login details have been saved. ")
print("You will now need to login")
Login()

def Login():
print("Please enter your details to log in")
username1 = input("Please enter your username: ")
password1 = input("Please enter your password: ")

file = open("Login.txt","r")

for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]
print(username,password)

if username1 == username and password1 == password:
print("Hello",username)

else:
print("incorrect")

#file.close()


user=input("Are you already a user? ")

if user == "Yes":
Login()

elif user =="No":
Register()

print("Welcome to our game")



I have entered the second user who is stored in the text file, It seems to be working but it checks my first entry and says its incorrect and then loops to the second entry. This is the output I keep getting:


Are you already a user? Yes
Please enter your details to log in
Please enter your username: jen
Please enter your password: ben
tess bess
incorrect
jen ben
Hello jen
Welcome to the dice game
>>>



Does anyone have an idea on how to only display the entry you have entered?



Thanks





delete the print(username,password) line ? Or put in the if clause
– Sharku
21 hours ago



print(username,password)





The issue is flow control. As a hint: for loops can use an else clause, which will execute if the entire loop completes without encountering a break (or return) statement. Maybe take a look into that.
– sytech
21 hours ago


for


else


break


return




3 Answers
3



As said by sytech, you could use the break and else clauses of the for loop:


break


else


for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]

if username1 == username and password1 == password:
print("Hello",username)
break
else:
print("incorrect")





Thank you for the help :)
– Ben Horton
17 hours ago



Like Sharku said, put the print(username,password) in your if below. Also writting clearly the name and password of the user after he typed it isn"t really a smart moove, delete it and just let your message when a user is logging in !


for row in file:
field = row.split(",")
username = field[0]
password = field[1]
lastchar = len(password)-1
password = password[0:lastchar]

if username1 == username and password1 == password:
print("Hello",username)

else:
print("incorrect")



Your 'for loop' will loop through each entry in your file, that means for each entry in the file your for loop prints the entry due to this line:


print(username,password)



If you don't want it to print all values in the file remove this line of code.
Adding a 'break' to your if statement, as suggested by others, will mean that as soon as your loop has found the entry that matches the one entered by the user it will leave the loop and not continue going through all values unnecessarily.



You could do something like this:


if username1 == username and password1 == password:
print("Hello",username)
break

else:
continue



This means that when a user input doesn't match an entry in the file the loop will just continue till it finds a match.
However, your code doesn't take into consideration if a user doesn't exist.





Thank you for the advice :)
– Ben Horton
17 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard