Python Forum

Full Version: Need help with code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I create a new account or retry login, It doesn't recognize and im unsure why

logged_in=False
attempts=0
 
enter_name=input('ENTER NAME: ')
enter_password=input('ENTER PASSWORD: ')
 
with open('Data.txt', 'r') as file:
    for line in file:
        name, password = line.split(',')
        if name == enter_name:
            logged_in = password == enter_password
            break
        else:
            attempts+=1
            print('WRONG USERNAME OR PASSWORD')
            enter_option=input('ENTER OPTIONS:\n A-RETRY\n B-NEW ACCOUNT\n')
            while enter_option=='A':
                enter_name=input('ENTER NAME: ')
                enter_password=input('ENTER PASSWORD: ')
                if name == enter_name:
                    logged_in = password == enter_password
                    break
                else:
                    attempts+=1
                    print('WRONG USERNAME OR PASSWORD')
                    enter_option=input('ENTER OPTIONS:\n A-RETRY\n B-NEW ACCOUNT\n')
            while enter_option=='B':
                with open('Data.txt', 'a') as file:
                     enter_new_name=input('ENTER NEW NAME: ')
                     enter_new_password=input('ENTER NEW PASSWORD: ')
                     file.write(enter_new_name+','+enter_new_password+"\n")
                     enter_name=input('ENTER NAME: ')
                     enter_password=input('ENTER PASSWORD: ')
                     if name == enter_name:
                        logged_in = password == enter_password
                        break
                     else:
                        attempts+=1
                        print('WRONG USERNAME OR PASSWORD')
                        enter_option=input('ENTER OPTIONS:\n A-RETRY\n B-NEW ACCOUNT\n')
    

            
if logged_in==True:
    print('Hello',name)
When you read from file the line has newline char at the end. It stays with password. You need to strip it in order yo match entered password.
(Apr-25-2019, 01:12 PM)buran Wrote: [ -> ]When you read from file the line has newline char at the end. It stays with password. You need to strip it in order yo match entered password.

How do I do that, I don't understand what you mean
(Apr-26-2019, 06:36 AM)SteampunkMaverick12 Wrote: [ -> ]
(Apr-25-2019, 01:12 PM)buran Wrote: [ -> ]When you read from file the line has newline char at the end. It stays with password. You need to strip it in order yo match entered password.

How do I do that, I don't understand what you mean

You iterate over rows in file (line #8). You split every row on comma to two separate values (name, password). Every line in file ends with newline character (otherwise there will be one long line). So every password you extract is in form mypassword\n. If you compare it to mypassword you will never get a match.