-2
username=input("Enter username ")
password=input("Enter password ")

with open("myfile.txt", "r") as username_finder:
    for lines in username_finder:
        if line.startswith('Username: '):
            if line.strip() == "Username: "+username and "Password: "+password:
                print("Correct")
            else:
                print("Sorry, this username or password does not exist")

The format in the file looks like this `Username: john password: psw123 '

When I put multiple users in to the file and then try the code it sayes the username and password it correct and incorrect for some reason

colkat406
  • 191
  • 1
  • 1
  • 10
  • 3
    `and "Password: "+password` will always succeed, since the truthiness of a non-empty string is always true. – Willem Van Onsem Nov 12 '18 at 20:48
  • what do u mean? – colkat406 Nov 12 '18 at 20:49
  • `for lines in username_finder:` should be `for line in username_finder:` without this typo I get expected behavior, can you show how you get both correct and incorrect output? Can you also show an example `Username: Password` line in the `myfile.txt`? The description suggests there are no data labels, but the code suggests there are data labels. – chickity china chinese chicken Nov 12 '18 at 20:57
  • @bob786 python if statements don't work like english. The line should be `if line.strip() == "Username: "+username and line.strip() == "Password: "+password:` – Joyal Mathew Nov 12 '18 at 20:59
  • @JoyalMathew How can that be right? How can the line be equal to two different strings? – Barmar Nov 12 '18 at 21:08
  • @Barmar Oh, you're right. I'm not completely sure what bob wanted there because `"Password: "+password` will always be `True`. – Joyal Mathew Nov 12 '18 at 21:11
  • sorry i asked for the wrong thing i meant if the input in the file was this username: john password: j123456 – colkat406 Nov 13 '18 at 20:58
  • also how would i loop it if they got the username or password wrong? – colkat406 Nov 14 '18 at 17:33

2 Answers2

2

If the file just contains the usernames and passwords, not the words Username: and Password, you should you should not have those strings in your equality check. It should be:

if line.strip() == username + ":" + password:

Also, you should not print that the name is not found in the else condition. The input will only match one line in the file, you'll print an error message for every other line. You should break out of the loop when you find a match, and print the error message in the else: clase of the for loop. See Searching array reports "not found" even though it's found

If you want to keep asking for the password until it's correct put everything in a loop.

check_failed = True
while check_failed:
    username = input("Enter username: ")
    password = input("Enter password: ")

    with open("myfile.txt") as username_finder:
        for line in username_finder:
            if (username + ": " + password) == line.strip():
                print("Correct")
                check_failed = False
                break;
        else:
            print("Sorry, this username or password does not exist")
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

First, let's analyze a part of your code.

The condition in your command

if line.strip() == "Username: "+username and "Password: "+password:

is possible to write with parentheses (by the operator precedence) as

(line.strip() == ("Username: " + username)) and ("Password: " + password)

Now, the right-hand operator of the resulting and operation

("Password: " + password)

is a nonempty string, and - consequently - evaluated as True (see Boolean operations), which makes your if statement the same as

if line.strip() == "Username: " + username

and, consequently, the password is never checked.


Now, to the logic of your program:

In your input file you don't have the words "Username: " and "Password: ", but something as

JohnDoe: pswd123

so the string literals "Username: " and "Password: " are totally inappropriate in your if statements. You wanted compare the entered username with the username in your file, and the entered password with the password in your file, in our example something like

if (username == "JohnDoe") and (password == "pswd123"):

or - following the format of lines in your input file -

if (username + ": " + password) == "JohnDoe: pswd123":

The right-hand part of == is simply the line of your input file, so your if statement may look as

if (username + ": " + password) == line:

so the (corrected) code of your program becomes

username = input("Enter username: ")
password = input("Enter password: ")

with open("myfile.txt") as username_finder:
    for line in username_finder:
        if (username + ": " + password) == line.strip():
            print("Correct")
            break;            # No need to search other lines   
    else:
        print("Sorry, this username or password does not exist")

Note the else branch of the for loop - it will perform only in the case when the loop is naturally exhausted, not interrupted by the break statement - see The for statement in the documentation.

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • Thank you for solving this for me and i understand what u are saying and now it makes a lot of more sense – colkat406 Nov 13 '18 at 20:34
  • Also one more question say that the input in the file is this username: JohnDoe password: pswd123 how would u make the code find the username password this way – colkat406 Nov 13 '18 at 20:49
  • Change the part `if (username + ": " + password)` to `if ("username: " + username + " password: " + password)`. Note the space symbol in "username: " and 2 space symbols in " password: ". – MarianD Nov 13 '18 at 21:36
  • how would i loop it if they got the username or password wrong? – colkat406 Nov 14 '18 at 17:29
  • I'm not sure I understand you, but I'll try to answer you anyway. How the program find out that the combination username/password is wrong? Only by exhausting the input file, i. e. the `for` loop. In which situation the `else:` branch will take on its role. – MarianD Nov 14 '18 at 22:17