0

This loop keeps looping even if I enter "no" and when I type "jdlfjap", for example, it continues to loop without a "?".

Does anyone know why this is?

def makeContact():
 contactName = input("Name: ")
 contactNumber = input("Number: ")
 dictionaryForContacts[contactName] = contactNumber
def continueMaking():
 while True:
 continueMaking = input("\nWould you like to continue making contacts? ")
 if continueMaking == "Yes" or "yes" or "YES":
 makeContact()
 continue
 elif continueMaking == "No" or "no" or "NO":
 break
 else: 
 print ("?")
 continue
asked Nov 7, 2015 at 22:34
5
  • continueMaking == "No" or "no" or "NO" doesn't do what you think it does. It doesn't compare continueMaking to each of those 3 words. Commented Nov 7, 2015 at 22:36
  • Use lower() or checkout in Commented Nov 7, 2015 at 22:37
  • Rewrite as if continueMaking in ("Yes", "yes", "YES") or if continueMaking.strip().lower() == "yes" Commented Nov 7, 2015 at 22:42
  • Possible duplicate of Compare multiple variables to the same value in "if" in Python? Commented Nov 7, 2015 at 22:45
  • You can try things in the interactive prompt: >>> "no" == "Yes" or "yes" or "YES" gives 'yes' Commented Nov 7, 2015 at 22:47

2 Answers 2

2

The statement if continueMaking == "Yes" or "yes" or "YES": is equivalent to (continueMaking == "Yes") or "yes" or "YES": which, regardless of the value of continueMaking returns the string "YES", which is truthy and thus the makeContact call always executes. Case-insensitive string comparisons can be accomplished by continueMaking.lower() == "yes".

answered Nov 7, 2015 at 22:40
Sign up to request clarification or add additional context in comments.

3 Comments

Great answer, but probably should be continueMaking.strip().lower() == "yes" to catch trailing spaces.
Based on operator precedence, why wouldn't it be evaluated to continueMaking == true since or has a higher precedence and it would be coerced into a boolean?
Nope, that statement simplifies to True if continueMaking == "Yes" else "yes".
1

Overwriting the function continueMaking with the variable continueMaking adds confusion. Choose a different variable name. Readability counts.

jwvh
51.3k7 gold badges42 silver badges70 bronze badges
answered Nov 7, 2015 at 22:44

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.