Python (LPTHW) Exercise 36

I am new to programming and am learning Python with the book Learning Python the Hard Way. I am in Exercise 36, where we are asked to write our own simple game.

http://learnpythonthehardway.org/book/ex36.html

(The problem is that when I am in the corridor (more precisely, in the “options”) and I write “gates”, the game responds as if I said “man” from the “old man”, etc.

What am I doing wrong?)

EDIT: I shouldn't have written "if" old man "or" person "by choice", but instead "choice" follows after each option.

However, the following problem. A man never gets up and does not smoke me, he frowns. Why doesn't the game go to this 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?" 

Thank you for your help:).

+5
source share
2 answers

The problem is that when I’m in the corridor (more precisely, in the “election”) and I write “gate”, the game reacts as if I said “man” “old man”, etc.

Description

What you were doing was viewed by Python as:

 if ("oldman") or ("man") or ("ask" in choice): 

Which evaluates to True if any of these three conditions has a "true" value . A non-empty string, such as "oldman" and "man", is True. So this explains why your code behaved the way it was done.

Check if there is a choice in the list of options:

You are looking for

 if choice in ['oldman', 'man', 'ask']: 

Or at least:

 if 'oldman' in choice or 'man' in choice or 'ask' in choice 
+2
source

Look carefully at the assessment of your conditions:

  if "oldman" or "man" or "ask" in choice: 

At first, this will be evaluated if "oldman" True , which happens, and the if condition will be evaluated as True .

I think you intended:

  if "oldman" in choice or "man" in choice or "ask" in choice: 

Alternatively you can use:

  if choice in ["oldman" , "man", "ask" ]: 

One caveat with this method is looking for an exact match, not a substring.

+3
source

Source: https://habr.com/ru/post/1239189/


All Articles