Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with code
#1
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)
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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
Reply
#4
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020