I'm new to programming and am learning Python with the book Learning Python the Hard Way. I'm at exercise 36, where we're asked to write our own simple game.
http://learnpythonthehardway.org/book/ex36.html
(The problem is, when I'm in the hallway (or more precisely, in 'choices') and I write 'gate' the game responds as if I said "man" of "oldman" etc.
What am I doing wrong?)
EDIT: I shouldn't have written 'if "oldman" or "man" in choice' but instead put 'in choice' after every option.
However, next problem.. The man never stands up and cursus me, it keeps frowning. Why does the game not proceed to that elif?
experiences = []
def choices():
while True:
choice = raw_input("What do you wish to do? ").lower()
if "oldman" in choice or "man" in choice or "ask" in choice:
print oldman
elif "gate" in choice or "fight" in choice or "try" in choice and not "pissedoff" in experiences:
print "The old man frowns. \"I said, you shall not pass through this gate until you prove yourself worthy!\""
experiences.append("pissedoff")
elif "gate" in choice or "fight" in choice or "try" in choice and "pissedoff" in experiences and not "reallypissed" in experiences:
print "The old man is fed up. \"I told you not to pass this gate untill you are worthy! Try me again and you will die.\""
experiences.append("reallypissed")
elif "gate" in choice or "fight" in choice or "try" in choice and "reallypissed" in experiences:
print "The old man stands up and curses you. You die"
exit()
elif "small" in choice:
print "You go into the small room"
firstroom()
else:
print "Come again?"
EDIT: FIXED IT!!
def choices():
while True:
choice = raw_input("What do you wish to do? ").lower()
if choice in ['oldman', 'man', 'ask']:
print oldman
elif choice in ['gate', 'fight', 'try'] and not 'pissedoff' in experiences:
print "The old man frowns. \"I said, you shall not pass through this gate until you prove yourself worthy!\""
experiences.append("pissedoff")
elif choice in ['gate', 'fight', 'try'] and not 'reallypissed' in experiences:
print "The old man is fed up. \"I told you not to pass this gate untill you are worthy! Try me again and you will die.\""
experiences.append("reallypissed")
elif choice in ['gate', 'fight', 'try'] and 'reallypissed' in experiences:
print "The old man stands up and curses you. You die"
exit()
elif "small" in choice:
print "You go into the small room"
firstroom()
else:
print "Come again?"
Thanks for all your help :).
Look carefully at the evaluation of your conditions:
if "oldman" or "man" or "ask" in choice:
This will first evaluate if "oldman"
is True
, which happens to be the case, and the if
condition will evaluate to be True
.
I think what you intended is:
if "oldman" in choice or "man" in choice or "ask" in choice:
Alternatively you could use:
if choice in ["oldman" , "man", "ask" ]:
The one caveat, is with this method it is looking for an exact match and not a substring match.
The problem is, when I'm in the hallway (or more precisely, in 'choices') and I write 'gate' the game responds as if I said "man" of "oldman" etc.
What you were doing was being seen by Python as:
if ("oldman") or ("man") or ("ask" in choice):
Which evaluates to True if any of those 3 conditions is of a "truthy" value. A non-empty string like "oldman" and "man" evaluates to True. So that explains why your code behaved the way it did.
You're looking for
if choice in ['oldman', 'man', 'ask']:
Or, at least:
if 'oldman' in choice or 'man' in choice or 'ask' in choice
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With