Python Forum
Need help with code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need help with code (/thread-17826.html)



Need help with code - SteampunkMaverick12 - Apr-25-2019

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)



RE: Need help with code - buran - Apr-25-2019

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.


RE: Need help with code - SteampunkMaverick12 - Apr-26-2019

(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


RE: Need help with code - perfringo - Apr-26-2019

(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.