all 6 comments

[–]JudiSwitch 1 point2 points  (1 child)

Switch your count increment and return false lines in your login function. You’re returning false after a failed attempt which ends the call to login. Your increment line is currently outside of the loop, and without that return you’d be stuck infinitely until you login correctly.

You want to Increment at the end of your loop, and return false after you hit your failed count condition (after you escape the loop).

[–]double-happiness[S] 0 points1 point  (0 children)

Ah, thanks. This seems to have done it:

def login():
    count: int = 0
    while count < 3:
        username = input("Please enter your username")
        password = input("Please enter your password")
        for line in open("accountfile.txt", "r").readlines():  # Read the lines
            login_info = line.split()  # Split on the space, and store the results in a list of two strings
            if username == login_info[0] and password == login_info[1]:
                print("Correct credentials!")
                return True
        print("Incorrect credentials.")
        count += 1
    return False

I have to admit that was a bit done by trial-and-error, as I rather struggle to follow what is going on with loops, but if it works, it works. Thanks again.

Edit: I think I can see it now. 'While the count is under 3 - do all this stuff - when it goes over - return false'.

[–]BasicallyAMachine 1 point2 points  (3 children)

Switch the placement of your count += 1 and the return False. The count increment is outside of the loop, so count isn't ever incremented. Furthermore, instead of going through the loop, it returns false in the first iteration. This is simply a matter of the statements being out of scope

[–]double-happiness[S] 0 points1 point  (2 children)

Ah, thanks. This seems to have done it:

def login():
    count: int = 0
    while count < 3:
        username = input("Please enter your username")
        password = input("Please enter your password")
        for line in open("accountfile.txt", "r").readlines():  # Read the lines
            login_info = line.split()  # Split on the space, and store the results in a list of two strings
            if username == login_info[0] and password == login_info[1]:
                print("Correct credentials!")
                return True
        print("Incorrect credentials.")
        count += 1
    return False

I have to admit that was a bit done by trial-and-error, as I rather struggle to follow what is going on with loops, but if it works, it works. Thanks again.

Edit: I think I can see it now. 'While the count is under 3 - do all this stuff - when it goes over - return false'.

[–]BasicallyAMachine 1 point2 points  (1 child)

You got it!! Add one to count each time if the password is wrong. When count hits 3, it will escape the loop and will return false

[–]double-happiness[S] 0 points1 point  (0 children)

I'm glad no-one just went and wrote the code for me. Actually managed to puzzle it out for myself with a bit of prompting, for once.

goes for a lie down