0

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."
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
plv
  • 949
  • 2
  • 8
  • 12
  • 3
    `x == a or b` is equivalent to `(x == a) or b`. Use `x == a or x == b` to get the desired results. There are too many duplicates .. –  Jan 16 '13 at 02:16
  • 2
    Or the shorter version: `if infoconf in ("N", "No"):` – Marius Jan 16 '13 at 02:23

4 Answers4

3

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
Jon Kartago Lamida
  • 827
  • 1
  • 7
  • 12
2

Python will interpret infoconf == "N" or "No" differently than you thought. This is somewhat a case of "operator precedence" where your condition is parsed as (infoconf == "N") or ("No").

Now, infoconf == "N" may or may not be true, but "No" is "something" and when treated as a logical evaluates as true. In effect, your condition infoconf == "N" or true will always be true.

As many others suggested, comparing infoconf to "No" in your second logical term will do the trick.

s.bandara
  • 5,636
  • 1
  • 21
  • 36
1

Personally I'd do something like this:

infoconf = raw_input("Is this information correct? Y/N: ")

if infoconf.lower().startswith('n'):
   # No
elif infoconf.lower().startswith('y'):
   # Yes
else:
   # Invalid

This means that the user could reply "Y/y/yes/yeah" for yes, and "N/n/no/nah" for no.

Alex L
  • 8,748
  • 5
  • 49
  • 75
1

In Python, it's a bit easier to do this as:

infoconf = raw_input("Is this information correct? Y/N: ")

if infoconf in ["N", "No"]:
    print "Please try again."
elif infoconf in ["Y", "Yes"]:
    start()
else:
    print "That is not a yes or no answer, please try again."

As others have said, if infoconf == "N" or "No" is equivilent to if (infoconf == "N") or "No", and since "No" (as a non-empty string) evaluates to True, the statement will always be true.

Also, to be a little less picky on the input, you might want to do infoconf = infoconf.strip().lower() before you do the tests (and then compare to the lower case versions).

tom10
  • 67,082
  • 10
  • 127
  • 137