-1

So I am working on a program that essentially asks a person if they want to hear a joke; if they say yes, then it will proceed to tell a joke; if they say no, or stop/quit the program will stop; and if they don't say yes or no, it will give them an error message. However, the program seems to just want to proceed on with the joke no matter what they input. I will produce an example of the code.

import sys

print("Want to hear a joke?")
answer = input()
if answer == "Yes" or "yes" or "y":
   print("Here is a joke")
elif answer == "No" or "no" or "n":
   sys.exit("Bye!")
else:
   print("Please input a different response.")

1 Answers1

1

This is NOT how you do a conditional

if answer == "Yes" or "yes" or "y": # this will always evaluate to true

do this instead

if answer in ["Yes" ,"yes" , "y"]:
      print "Here is a joke"
jramirez
  • 8,537
  • 7
  • 33
  • 46
  • Thanks so much, that worked out for me. Then I found out input() caused an error message that said Answer wasn't defined, so I had to use raw_input(). But nonetheless I got it to work. Thanks again. – Hunter Watkins Sep 05 '14 at 21:52