1

I have a while loop and if you answer the question wrong, it repeats the question, but when I get it right first time it ask again and it works the second time. code:

def signup():
    signup=input("Enter a username: ")
    signup2=input("Enter a password: ")

def login():
    login=input("Enter your username: ")
    login2=input("Enter your password: ")

option=input("Would you like to login or signup?: ")

while option != "signup" or option != "login":

    option=input("Would you like to login or signup?: ")

    if option == "signup":
        signup()
    elif option == "login":
        login()

and the response is:

Would you like to login or signup?: signup
Would you like to login or signup?: signup
Enter a username:
martineau
  • 119,623
  • 25
  • 170
  • 301
BossDog _
  • 105
  • 1
  • 1
  • 3
  • 2
    You ask for input twice. Once before the loop, and again inside the loop. – Barmar Oct 14 '19 at 16:10
  • 1
    If `option == "signup"`, it is guaranteed that `option != "login"`. Use `and`, or use `not (option == "signup" and option == "login"). See https://en.wikipedia.org/wiki/De_Morgan's_laws. – chepner Oct 14 '19 at 16:11
  • This is a good case to do some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – Some programmer dude Oct 14 '19 at 16:11

3 Answers3

1

Your condition is incorrect. If option does equal one or the other, the other comparison is guaranteed to be true. You want to use and, not or

while option != "signup" and option != "login":

or by De Morgan's laws

while not (option == "signup" or option == "login"):

The best solution, though, is to use an "infinite" loop with an explicit break statement, so that you only need to write the call to input once.

while True:

    option=input("Would you like to login or signup?: ")

    if option == "signup" or option == "login":
        break


if option == "signup":
    signup()
elif option == "login":
    login()
chepner
  • 497,756
  • 71
  • 530
  • 681
0

Of course, since you also place the option=input("Would you like to login or signup?: ") outside the loop first. To make this kind of loop, it's better to make an infinite loop and to break on a condition:

while True:

    option=input("Would you like to login or signup?: ")

    if option == "signup":
        signup()
        break
    elif option == "login":
        login()
        break
Mathieu
  • 5,410
  • 6
  • 28
  • 55
0

You're asking for input a second time inside the loop. You should only do that if the input wasn't one of the valid choices.

You should use and rather than or to test the input. See Why non-equality check of one variable against many values always returns true?

Finally, you should process the input after the loop, since that's when it will contain a valid choice.

option=input("Would you like to login or signup?: ")
while option != "signup" and option != "login":
    option=input("Would you like to login or signup?: ")

if option == "signup":
    signup()
elif option == "login":
    login()
Barmar
  • 741,623
  • 53
  • 500
  • 612