Branches and Functions
You have learned to do if- statements, functions, and lists. Now it’s time to bend your mind. Type this in, and see if you can figure out what it’s doing.
1 from sys import exit
2
3 def gold_room():
4 print "This room is full of gold. How much do you take?"
5
6 next = raw_input("> ")
7 if "0" in next or "1" in next:
8 how_much = int(next)
9 else:
10 dead("Man, learn to type a number.")
11
12 if how_much < 50:
13 print "Nice, you're not greedy, you win!"
14 exit(0)
15 else:
16 dead("You greedy bastard!")
17
18
19 def bear_room():
20 print "There is a bear here."
21 print "The bear has a bunch of honey."
22 print "The fat bear is in front of another door."
23 print "How are you going to move the bear?"
24 bear_moved = False
25
26 while True:
27 next = raw_input("> ")
28
29 if next == "take honey":
30 dead("The bear looks at you then slaps your face off.")
31 elif next == "taunt bear" and not bear_moved:
32 print "The bear has moved from the door. You can go through it now."
33 bear_moved = True
34 elif next == "taunt bear" and bear_moved:
35 dead("The bear gets pissed off and chews your leg off.")
36 elif next == "open door" and bear_moved:
37 gold_room()
38 else:
39 print "I got no idea what that means."
40
41
42 def cthulhu_room():
43 print "Here you see the great evil Cthulhu."
44 print "He, it, whatever stares at you and you go insane."
45 print "Do you flee for your life or eat your head?"
46
47 next = raw_input("> ")
48
49 if "flee" in next:
50 start()
51 elif "head" in next:
52 dead("Well that was tasty!")
53 else:
54 cthulhu_room()
55
56
57 def dead(why):
58 print why, "Good job!"
59 exit(0)
60
61 def start():
62 print "You are in a dark room."
63 print "There is a door to your right and left."
64 print "Which one do you take?"
65
66 next = raw_input("> ")
67
68 if next == "left":
69 bear_room()
70 elif next == "right":
71 cthulhu_room()
72 else:
73 dead("You stumble around the room until you starve.")
74
75
76 start()
Output
$ python ex35.py
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?
> taunt bear
The bear has moved from the door. You can go through it now.
> open door
This room is full of gold. How much do you take?
> 1000
You greedy bastard! Good job!
Study Drills
- Draw a map of the game and how you fl ow through it.
- Fix all your mistakes, including spelling mistakes.
- Write comments for the functions you do not understand. Remember doc comments?
- Add more to the game. What can you do to both simplify and expand it?
- The gold_room has a weird way of getting you to type a number. What are all the bugs in this way of doing it? Can you make it better than just checking if “1” or “0” are in the number? Look at how int() works for clues.
Common Student Questions
Help! How does this program work!?
Any time you get stuck understanding a piece of software, simply write an English comment above every line, explaining what it does. As you go through doing this, correct comments that aren’t right, based on new information. Then when you’re done, try to either diagram how it works or write a paragraph or two describing it. If you do that, you’ll get it.
Why are you doing while True:?
That makes an infinite loop.
What does exit(0) do?
On many operating systems, a program can abort with exit(0), and the number passed in will indicate an error or not. If you do exit(1), then it will be an error, but exit(0) will be a good exit. The reason it’s backward from normal boolean logic (with 0==False) is that you can use different numbers to indicate different error results. You can do exit(100) for a different error result than exit(2) or exit(1).