I am a total n00b to both programming and Python. I am working my way through "Python Programming for the Absolute Beginner" by Michael Dawson. On of the "challenges" given on page 87, chapter 3 is to create a Fortune cookie program. I can't get the code I want to execute when a "negative" or jibberish reply is put as "response" via "raw input". I'm sure its something glaring and stupid so don't bit my head off, lol. Here's the code:

# fortune cookie...Chinese-food-guy begs you to let him read your fortune

import random

print "This is Ching-wa Chui. Please, let me crack a fortune cookie open for you!\n"

response = raw_input("response: ")

# if affirmative answer is given, then fortune is read

if response == "yes" or "OK" or "Yes" or "ok":
fortune = random.randrange(5)
if fortune == 0:
print "You are going to have some good bang-bang tonight!"

elif fortune == 1:
print "I think you had better stay here. It says you are about to be killed!"

elif fortune == 2:
print "You are going to have a long, prosperous life. How generic. Oh well."

elif fortune == 3:
print "DUCK!!!! *bullet whizzes by*"

elif fortune == 4:
print "You will see great riches..... of the spiritual variety."

#if negative answer given, then plead for affirmative answer

elif response == "no" or "no thanks" or "No":
print "come on...pleeeeeeeaaaaseee?\n"
response = raw_input("response: ")

# if random word typed, ask again.

else:
print "Excuse me? I SAID...'DO YOU WANT ME TO OPEN A FORTUNE COOKIE FOR YOU?!"
response = raw_input("response: ")

As you can see, the "elif" and "else" blocks don't execute. What's going wrong? Thanks in advance for all your help.

Recommended Answers

All 2 Replies

Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

[code=python]
your Python code here

[/code]

This code will not work:

if response == "yes" or "OK" or "Yes" or "ok":

I has to be recoded to:

if response in ["yes", "OK", "Yes", "ok"]:

Ok, thank you for your help. Sorry about that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.