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
124k29 gold badges181 silver badges319 bronze badges
asked Oct 14, 2019 at 16:08
3
  • 2
    You ask for input twice. Once before the loop, and again inside the loop. Commented Oct 14, 2019 at 16:10
  • 1
    If option == "signup", it is guaranteed that option != "login". Use and, or use `not (option == "signup" and option == "login"). See en.wikipedia.org/wiki/De_Morgan's_laws. Commented Oct 14, 2019 at 16:11
  • This is a good case to do some rubber duck debugging. Commented Oct 14, 2019 at 16:11

3 Answers 3

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()
answered Oct 14, 2019 at 16:11
Sign up to request clarification or add additional context in comments.

Comments

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
answered Oct 14, 2019 at 16:12

Comments

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()
answered Oct 14, 2019 at 16:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.