1

Using python 2.7.5

x = (raw_input)'''What will you Reply?
a. Nod
b. Answer
c. Stare
'''))

if x == "Nod" or "a" or "nod":
    print '''You nod shyly.
"Oh that's Great"'''
elif x == "Answer" or "b" or "answer":
    print '''You answer in a low tone.
"You can speak!"'''
elif x == "Stare" or "c" or "stare":
    print '''you stare blankly, so does the AI.
"Well okay then."'''

When i then run it no matter what i put in the prompt, it will only trigger '''You nod shyly. "Oh Thats great"'''

But then if i copy this and paste it into my python shell, it has a problem with "Oh" and if i get rid of that, it has a problem with t in "That's" and if i just get rid of "That's great" it has a problem with the first three characters of the next elif statement. WTF is wrong, my python code and shell have been working fine lately, being able to split if and elif. but now all of a sudden it just doesn't want to.

zhangyangyu
  • 8,520
  • 2
  • 33
  • 43

3 Answers3

5
if x == "Nod" or "a" or "nod"

is parsed as

if (x == "Nod") or ("a") or ("nod"):

"a" is Truish, so the condition is always True, whether or not x == "Nod".

Instead you could use:

if x in ("Nod", "a", "nod"):
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
1
if x == "Nod" or "a" or "nod":

This will always lead to True.

You should use

if x in ["Nod", "a", "nod"]

Or

if x == "Nod" or x == "a" or x == "nod"
zhangyangyu
  • 8,520
  • 2
  • 33
  • 43
0

Your first condition:

if x == "Nod" or "a" or "nod":

Is always evaluted as true. Try this code:

x = raw_input('What will you Reply? a. Nod b. Answer c. Star ')

if x in ["Nod", "a", "nod"]:
    print '''You nod shyly. "Oh that's Great"'''
elif x in ["Answer", "b", "answer"]:
    print '''You answer in a low tone. "You can speak!"'''
elif x in ["Stare", "c", "stare"]:
    print '''you stare blankly, so does the AI. "Well okay then."'''
amatellanes
  • 3,645
  • 2
  • 17
  • 19
  • ah yay, thanks so much, it works now. i just want to know why it was causing a different output. and causing my shell get pissed at characters of the code. – user2625538 Jul 27 '13 at 12:39