-2

After I type in any username and password (doesn't matter whether it's correct or not), it keeps printing another part of the code.

The login part works fine but it doesn't show the correct output afterwards. It continuously shows: "Incorrect login details entered Have you made an account? Yes or No"

This has stumped both my teacher and I. I have looked at different websites with examples of login/registration systems. I have also tried re-arranging the code differently.

This is the code:

username = input("Please enter your username: ")
password = input("Please enter your password: ")
file = open("Usernames.txt","r")
found = False
for line in file:
    user = line.split(",")
    if user[0] == username and user[1] == password:
        found = True
        print("Username: " + user[0])
        print("~~~~~")
        print("Welcome to the game, " + user[0])
    else:
        found == False
        print("Incorrect login details entered")
        print("Have you made an account?")
        ans = input("Yes or No ")
        while ans not in ("Yes", "No"):
            if ans == "Yes":
                print ("Please sign in again")
                username = input("Please enter your correct username: ")
                password = input("Please enter your correct password: ")
            elif ans == "No":
                print("Would you like to make an account? ")
            else:
                ans = input("Please enter Yes or No ")

The expected result when the username and password is correct:

Username: Smartic
~~~~~
Welcome to the game, Smartic

The expected result when the username and password is incorrect:

Incorrect login details entered
Have you made an account?
Yes or No

The expected result when the user enters Yes:

Please sign in again
Please enter your correct username:
Please enter your correct password:

The expected result when the user enters No:

Would you like to make an account?

The expected result when the user enters something other than Yes or No:

Please enter Yes or No 
  • 1
    The problem is that for every username in the file, if it doesn't match the one you entered it tells you "("Incorrect login details entered")" You should remove the 'Else' statement and put everything below it outside of the 'For' loop. – JeffUK May 02 '19 at 15:37
  • 1
    Most of the code is indented underneath the `for line in file` loop. All of that code will be repeated for each line in the file. How many lines are in that file? – John Gordon May 02 '19 at 15:40
  • I hammered this closed -- but make sure you read through the duplicate's answers to find the nuggets you need. Your problem isn't how to handle the outer loop -- it's that you have to *complete* the validity test before you decide whether the input is valid. Check name and password for *all* file entries before you reject. This is included in one of the duplicate's examples, as well as in the answers here. – Prune May 02 '19 at 16:55
  • See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. Simple debugging to trace your accept-reject decision would have shown the problem much earlier. – Prune May 02 '19 at 16:56
  • thank you very much for the help :) – Vanessa Rojas May 02 '19 at 21:28

3 Answers3

0

No matter what username you enter, it won't satisfy every username in the file. Every time it doesn't, your error text will be printed. Reformat your code as is described in this question.

Alec
  • 8,529
  • 8
  • 37
  • 63
0

Change your:

while ans not in ("Yes", "No"):

In:

while True:

Besides I can recommend to make a function.

Also as John Gordon mentioned use breaks, so your script will look like:

username = input("Please enter your username: ")
password = input("Please enter your password: ")
user = {0:'e',1:'w'}
found = False

if user[0] == username and user[1] == password:
    found = True
    print("Username: " + user[0])
    print("~~~~~")
    print("Welcome to the game, " + user[0])
else:
    found == False
    print("Incorrect login details entered")
    print("Have you made an account?")
    ans = input("Yes or No ")
    while True:
        if ans == "Yes":
            print ("Please sign in again")
            username = input("Please enter your correct username: ")
            password = input("Please enter your correct password: ")
            break
        elif ans == "No":
            print("Would you like to make an account? ")
            break
        else:
            ans = input("Please enter Yes or No ")
            break
Dylan_w
  • 472
  • 5
  • 19
0

There is a new line at the end of each line in the file. You can remove newline by using strip() function.

if user[0] == username and user[1].strip() == password:
    found = True
    print("Username: " + user[0])
    print("~~~~~")
    print("Welcome to the game, " + user[0])
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38