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
2 Answers 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".
3 Comments
continueMaking.strip().lower() == "yes" to catch trailing spaces.continueMaking == true since or has a higher precedence and it would be coerced into a boolean?True if continueMaking == "Yes" else "yes".Overwriting the function continueMaking with the variable continueMaking adds confusion. Choose a different variable name. Readability counts.
continueMaking == "No" or "no" or "NO"doesn't do what you think it does. It doesn't comparecontinueMakingto each of those 3 words.lower()or checkoutinif continueMaking in ("Yes", "yes", "YES")orif continueMaking.strip().lower() == "yes">>> "no" == "Yes" or "yes" or "YES"gives'yes'