Home > Software engineering >  Why won't my print statements show up in my elif block?
Why won't my print statements show up in my elif block?

Time:02-17

I know there is probably a simple solution to this, but the text I wrote under the elif sections in userName and passWord will not print when a user successfully logs in. Please help!

def userName():
    username = "kate"
    userInput = input('Please enter your username:\n')

    while userInput != username:

        if len(userInput) == 0:
            print("Your username can not be blank, please try again!\n")
            userInput = input('Please enter your username.\n')
        elif userInput == username:
            print("Welcome back, "   username)
            print("")
            break
        else:
            print("Sorry, that username is not in our system. Please try again!\n")
            userInput = input('Please enter your username.\n')

def passWord():
    password = "stags"
    passInput = input("Please enter your password:\n")

    while passInput != password:

        if len(passInput) == 0:
            print("Your password can not be blank, please try again!\n")
            passInput = input("Please enter your password.\n")
        elif passInput == password:
            print("You have successfully logged in!\n")
            break
        else:
            print("Sorry, your password is invalid. Please try again!")
            passInput = input("Please enter your password.\n")

def main():
    print("Hello, let's get started!")
    print("")
    userName()
    passWord()

main()

CodePudding user response:

This was pointed out in the comments above, but if you change

while userInput != username:

and

while passInput != password:

to

while True:

it should work just fine. Then it forces your code to hit the elif statement rather than breaking the loop before printing what you want to say.

CodePudding user response:

In python indentation indicates where a code block starts and begins. So in your while loop in passWord() your if..elif..else must be indented exactly one tab in eg.

    while passInput != password:

        if len(passInput) == 0:
            print("Your password can not be blank, please try again!\n")
            passInput = input("Please enter your password.\n")
        elif passInput == password:
            print("You have successfully logged in!\n")
            break
        else:
            print("Sorry, your password is invalid. Please try again!")
            passInput = input("Please enter your password.\n")

Notice how the indentation always goes one tab in for each code block you want to create.

  • Related