Search code examples
pythonif-statementsyntaxor-operator

If-branch for x == "N" or "No" always runs


When I run this in my program, the question goes through, however no matter the answer, the "No" option always runs. If I switch the option order, the "Y" option will only run and it will always go straight to start. I'm sure I'm missing something simple, I just don't know what.

infoconf = raw_input("Is this information correct? Y/N: ")
    
    if infoconf == "N" or "No":
        print "Please try again."
    elif infoconf == "Y" or "Yes":
        start()
    else:
        print "That is not a yes or no answer, please try again."

Solution

  • Supposed to be

    infoconf = raw_input("Is this information correct? Y/N: ")
    
    #you wrote:  infoconf == "N" or "No" but should be:
    if infoconf == "N" or infoconf == "No": 
      print "Please try again."
    #you wrote: infoconf == "Y" or "Yes" but should be
    elif infoconf == "Y" or infoconf == "Yes": 
      start()
    else:
      print "That is not a yes or no answer, please try again."
    

    Short explanation:

    when value of x = 'N'
    x == 'N' or 'No' will return True
    when value of x = 'Y'
    x == 'N' or 'No' will return 'No' i believe this is not what you want
    

    at the other side

    when value of x = 'N'
    x == 'N' or x == 'No' will return True
    when value of x = 'Y'
    x == 'N' or x == 'No' will return False i believe this is what you want