I'm trying to practice my python skills, I knew that I was kind of unfamiliar with how to work with files so I decided to teach my self. The code is basically making an account, signing in and checking the user and pass stored in a file. I know my user and pass is appending and reading to the file. The problem i'm getting is in my if statement. I'm using if i == user
but == means literal. So i tried just using one = to check if one variable = the variable that the user has put in, when I do that, I get a syntax error. My last question is when I run the code, in they else statement where it says username incorrect, it says it 2 or 3 times in the console. This confuses me because no where outside of the else statement is their a while or for loop. If someone can just explain how to get my if statment to excutute when the user and password string are right. That would be great. Also if you know why my console is doing the else: print('username incorrect) 3 times for no reason. I'm very curious to know
I tried printing out the variables in my for loop and it indeed printed the strings in the file.
u = open('users.txt', 'a+')
p = open('pass.txt', 'a+')
does_acc = input('Do you have a account? ')
if does_acc == 'no' or does_acc == 'No':
new_user = input('Username: ')
u.write(new_user + '\n')
new_pass = input('Password: ')
p.write(new_pass + '\n')
sign_in = input('Would you like to sign in? ')
if sign_in == 'yes' or sign_in == 'Yes':
n = open('users.txt', 'r')
m = open('pass.txt', 'r')
lines = n.readlines()
line = m.readlines()
user = input('Username: ')
for i in lines:
print(i)
if i = user:
password = input('Password: ')
for x in lines:
if password == x:
print('secret file opened')
else:
print("{} is not a valid password.").format(password)
else:
print('Username incorrect.')
else:
print('Have a nice day.')
u.close()
p.close()
Do you have a account? yes
Would you like to sign in? yes
Bob
Username: Bob
Username incorrect.
Username incorrect.