5

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 :).

Marijke Vonk
  • 418
  • 3
  • 12
  • I want the old man to stand up and curse after I write 'gate' three times. After the first time 'pissedoff' is added to the experiences list, if I input gate again it should warn me and add reallypissed to the experiences list, and if I input 'gate' the third time it should exit. – Marijke Vonk Dec 25 '15 at 08:16
  • Hey I have suggestion: can you rewrite the if/elif using `if choice in [..., ...]` to make it easier to follow? I'll be back in a bit to have a look! – bakkal Dec 25 '15 at 08:24
  • I did, and I think I fixed it! Thanks so much for your help :). – Marijke Vonk Dec 25 '15 at 08:25

2 Answers2

3

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.

David Maust
  • 8,080
  • 3
  • 32
  • 36
2

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.

Explanation

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.

Test if a choice is in a list of choices:

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
bakkal
  • 54,350
  • 12
  • 131
  • 107