1

I am writing a code to implement a user login system, in which the user will enter his details like name and pass which will be stored in a text file. When he will try to login it will ask for the username and password and matches with the one stored in the text file created earlier.

Prob is it is not able to search in text file correctly

user=str(input('Do u have registration(Y/N): \n '))
if user =='Y':
    print('Please login now:')
    boom=str(input("Enter your name: "))
    pass_inp=str(input('Enter your password: \n'))


    with open('user_database.txt','r') as f:
        if pass_inp in f.readlines() and boom in f.readlines():
            print('Welcome User')
        else:
            print('Invalid Login ID')
elif user == 'N':
    print('Please register now,fill the following details:')
    name=str(input("Register your name: \n"))
    password =str(input('Create your password:\n'))
    with open("user_database.txt",'a') as f:
        f.write(name+'\n')

        f.write(password+'\n')

        print('Account successfully created')`````````

3 Answers3

0

Putting aside the reason for saving password and user as plain text in a file and allowing for username and password to be the same as well, Please check if '\n' in f.write(name+'\n')is causing the issue.(certainly is)

skott
  • 554
  • 3
  • 9
0

You are reading the text file twice with

if pass_inp in f.readlines() and boom in f.readlines(): 

Set contents to a variable and then check the variable for name and password.

contents =  f.readlines()

if pass_inp in contents and boom in contents:

Previous solution to this problem. Advantage of this solution is that with a small coding change it is able to distinguish user names and passwords when checking which your current approach does not.

DarrylG
  • 16,732
  • 2
  • 17
  • 23
0

Hope comments helps you with understanding the logic:

# input return a string
user = input('Do u have registration(Y/N): \n ')
if user == 'Y':
    print('Please login now:')
    boom = input("Enter your name: ")
    pass_inp = input('Enter your password: \n')

    with open('user_database.txt','r') as f:
        lines = f.readlines()
        flag_founded = False
        for index, line in enumerate(lines):
            # user name found check if the line after it contains it's password
            if boom == line.replace('\n', ''):  
                # password of this user should be in the next line
                password = lines[index+1].replace('\n', '')
                if pass_inp == password:
                    flag_founded = True
                break  # break the current loop no need to keep checking because user name should be unique
        if flag_founded:
            print('You are logged: ', boom)
        else:
            print('User name or password wrong')

elif user == 'N':
    print('Please register now,fill the following details:')
    name = input("Register your name: \n")
    password = input('Create your password:\n')
    with open("user_database.txt", 'a') as f:
        f.write(name+'\n')
        f.write(password+'\n')
        print('Account successfully created')
Charif DZ
  • 14,415
  • 3
  • 21
  • 40