Puneeth Allapu
Puneeth Allapu

Reputation: 1

How To Search through a File & check if there is

I am going for a user login system. Plz say if I have declared a variable for my text file(database)

# Global Variables (few are inside function)
global data
global l_password
global wrong


# The Main Thing
def system():
    choose = input("Enter 'c' to create an account \n\t\tor \nEnter 'l' to log in \n\nEnter here: ")
    if choose == "c":
        create()
        print("Now please login with your created account")
        login()
    elif choose == "l":
        login()
    else:
        print("You have entered and invalid character. \nPlease enter only 'c' or 'l' ")


# Creating an account
def create():
    c_username = str(input("Username: "))
    c_password = str(input("Password: "))
    data = open("accounts_data", "a")
    data.write("\n\nUsername: ")
    data.write(c_username)
    data.write("\nPassword: ")
    data.write(c_password)
    print("Success! Account is created.")


# Logging in an account
def login():
    print("Login with your account ---->")
    l_username = str(input("Username: "))
    l_password = str(input("Password: "))

    if l_username or l_password in data.read():
        print("Congrats! You are successfully now logged into your account!")

    if l_username or l_password not in data.read():
        error = str(input(
            "Sorry we couldn't find your account. "
            "\nEnter the correct username or password."
            "\n\tor \nType 'r' to retry \nor \nCreate one by typing 'c'\n:"))

        if error == 'r':
            login()

        elif error == 'c':
            create()
            login()


system()
data.close()

It is saying both, logged into the account and also sorry couldn't find an account

    if l_username or l_password in data.read():
        print("Congrats! You are successfully now logged into your account!")

    if l_username or l_password not in data.read():
        error = str(input(
            "Sorry we couldn't find your account. "
            "\nEnter the correct username or password."
            "\n\tor \nType 'r' to retry \nor \nCreate one by typing 'c'\n:"))

MY TEXT FILE(where these all accounts are stored and the variable is data for this file) Please say me if I have declared a variable for this file

my text file has all this. Is it right?

Username: apple
Password: apples

Upvotes: 0

Views: 59

Answers (3)

Giovanna Fritsche
Giovanna Fritsche

Reputation: 1

Try to use:

    line = data.read()
    if l_username in line and l_password in line:
            print("Congrats! You are successfully now logged into your account!")

The value inside l_username is valid; in this situation is always true. You are only comparing the l_password to the read line in data.read(). It is why you are getting both messages.

You will probably have trouble with your method to read the file. I suggest you maintain the data in a local variable or read the entire file in every login.

Another critical point here is that you do not differ in l_username and l_password compared to the data.read(). If the user inserts the login value in the password, it also is going to be true.

Upvotes: 0

bbbbbbbbbroc
bbbbbbbbbroc

Reputation: 13

There is obviously a problem here:

 if l_username or l_password in data.read():
        print("Congrats! You are successfully now logged into your account!")

  if l_username or l_password not in data.read():
        error = str(input(
            "Sorry we couldn't find your account. "
            "\nEnter the correct username or password."
            "\n\tor \nType 'r' to retry \nor \nCreate one by typing 'c'\n:"))

Login is by username and password, so in your code, the or operator on the top would mean, if either the username or password was correct, you would get to login. The output being both is caused by either the password or username being correct, the other being incorrect.

The final solution? Just replace the or in the first statement with and

Upvotes: 0

khushi
khushi

Reputation: 365

This is happening because of the 'or' you have used in the first part.

if l_username and l_password in data.read():
        print("Congrats! You are successfully now logged into your account!")

please replace the 'or' with an 'and'like I have done above and tell me if that works. (leave the or in the second part as it is, just change this particular one to 'and')

Upvotes: 1

Related Questions