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
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".

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.

Comments

Your Answer

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.