0

I am writing a code which involves a login subroutine and the it works well apart from the fact that it gives two outputs when I only want one of them. The Subroutine is as follows:

def Login():

    chances = 0 
    Status = True #Status refers to whether the person is logged in or not

    while Status is True:
        Supplied_Username = input('What is your username?')
        Supplied_Password = input('What is your password?')
        with open("Loginfile.txt","r") as Login_Finder:
            for x in range(0,100):

                for line in Login_Finder:

                    if (Supplied_Username + ',' + Supplied_Password) == line.strip():  
                        print("You are logged in")
                        game()
            else:
                print("Sorry, this username or password does not exist please try again")
                chances = chances + 1
                if chances == 3:
                    print("----------------------------------------------------\n Wait 15 Seconds")
                    time.sleep(15)
                    Login()
                    sys.exit()

def game():
    print('HI')

This works well like I said above. When the user inputs the correct details, they get both the:

'You are logged in' output and the 'Sorry... these details don't exist' output

What do I need to do to make sure I get the correct output for every scenario (wrong details and correct details)?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Kabz
  • 1
  • 1
    What's the purpose of the `for x in range(0, 100):` loop? You never use `x` for anything. – Barmar Dec 20 '19 at 01:37
  • 1
    The `else:` block of a loop is executed if the loop ends normally instead of stopping with `break`. So you need to break out of the loop when you find what you're looking for. – Barmar Dec 20 '19 at 01:38
  • 1
    Uppercase is conventionally not used in variable names in Python – Mad Physicist Dec 20 '19 at 01:38
  • 1
    @MadPhysicist The question specifically asks about output, not return values. – Barmar Dec 20 '19 at 01:40
  • 1
    Every time you call `Login()` recursively, it sets `chances` to `0`, so it will never reach 3. – Barmar Dec 20 '19 at 01:41
  • Your "else" is not indent well with the "if". Is this a format issue or you intended? Try after indent them to see if any difference. – Yingyu YOU Dec 20 '19 at 01:44
  • Related: [Why does python use 'else' after for and while loops?](https://stackoverflow.com/q/9979970/4518341) – wjandrea Dec 20 '19 at 01:47
  • @Yingyu `else` after a `for` block is valid, and it would make sense if OP just put a `break` in the loop. – wjandrea Dec 20 '19 at 01:48
  • Your issue is that after calling `game()` the rest of your code continues to run. You can fix this by adding `return game()` which will exit the function when game is started. – Myccha Dec 20 '19 at 02:00
  • @wjandrea Thanks for pointing this! I think I totally misunderstood the problem itself without knowing the usage of for-else syntax. Thanks. – Yingyu YOU Dec 20 '19 at 08:13

1 Answers1

0

I made a few changes to your code keeping the functionality the same, just showing some python best practices.

(Note that in python capitalized names are reserved for class names by convention, snake case is used for variable and function names).

def login(remaining_chances=3, delay=15):
    '''Requests username and password and starts game if they are in "Loginfile.txt"'''

    username = input('What is your username?')
    password = input('What is your password?')

    with open("Loginfile.txt","r") as login_finder:
        for line in login_finder.readlines():
            if line.strip() == f'{username},{password}':
                print("You are logged in")
                game()

    print("Sorry, this username or password does not exist please try again")

    remaining_chances -= 1

    if remaining_chances == 0:
        print(f"----------------------------------------------------\n Wait {delay} Seconds")
        time.sleep(delay)
        login(delay=delay)
    else:
        login(remaining_chances=remaining_chances, delay=delay)

However, this does not solve your problem.

The issue is that you are not exiting this function. In python the function should return something when it finishes, but here you are either starting the game or throwing an exception. In many ways, this function is never meant to have a return value.

Maybe you want to return game(). This exits the function and calls game()

Myccha
  • 961
  • 1
  • 11
  • 20