1

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.
Zoe
  • 27,060
  • 21
  • 118
  • 148
Nick_M
  • 17
  • 2

2 Answers2

2
  1. This if i = user: should be replaced with this: if i == user: (double equal sign)
  2. Once you have created a new account, you should close the files, because you're going to open them again while they are already open. This might or might not lead to a wrongfully read / written data.
  3. The idea that once user has entered their name, the whole password file is matched against the input password does not seem right -- you only need to check the one password for every user.
  4. for x in lines: tries to match the password against the user names. Seems strange.
  5. print("{} is not a valid password.").format(password) has wrong parenthesis order, it should be "string".format(parm) enclosed in print( .... ), not vice versa.

All in all, I'd rather rewrite your password matching part like this:

with open( 'passwords.txt' ) as fin :
    passwords = [p.strip() for p in fin.readlines()]

with open( 'users.txt' ) as fin :
    users = [u.strip() for u in fin.readlines()]

user = input( 'User: ' )
password = input( 'Password: ' )

if [user, password] in zip( users, passwords ) :
    print( "You're welcome!" )
else :
    print( "Go away!" )

Or something along the lines.

lenik
  • 23,228
  • 4
  • 34
  • 43
  • Unless they're running an experimental version of Python, `if i = user` should generate a syntax error. I suspect a copy/paste error. – Mark Ransom Aug 18 '19 at 02:52
0

the if i = user should be if i == user and also you are opening the same file twice, one at the beginning and the second one after if sign_in == 'yes' or sign_in == 'Yes':

Torgon
  • 111
  • 8