Exercise 35. Branches and Functions

You have learned 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.

ex35.py


 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        choice = input("> ")
 7        if "0" in choice or "1" in choice:
 8            how_much = int(choice)
 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            choice = input("> ")
28
29            if choice == "take honey":
30                dead("The bear looks at you then slaps your face off.")
31            elif choice == "taunt bear" and not bear_moved:
32                print("The bear has moved from the door.")
33                print("You can go through it now.")
34                bear_moved = True
35            elif choice == "taunt bear" and bear_moved:
36                dead("The bear gets pissed off and chews your leg off.")
37            elif choice == "open door" and bear_moved:
38                gold_room()
39            else:
40                print("I got no idea what that means.")
41
42
43    def cthulhu_room():
44        print("Here you see the great evil Cthulhu.")
45        print("He, it, whatever stares at you and you go insane.")
46        print("Do you flee for your life or eat your head?")
47
48        choice = input("> ")
49
50        if "flee" in choice:
51            start()
52        elif "head" in choice:
53            dead("Well that was tasty!")
54        else:
55            cthulhu_room()
56
57
58    def dead(why):
59        print(why, "Good job!")
60        exit(0)
61
62    def start():
63        print("You are in a dark room.")
64        print("There is a door to your right and left.")
65        print("Which one do you take?")
66
67        choice = input("> ")
68
69        if choice == "left":
70            bear_room()
71        elif choice == "right":
72            cthulhu_room()
73        else:
74            dead("You stumble around the room until you starve.")
75
76
77    start()

What You Should See

Here’s me playing the game:

Exercise 35 Session


$ python3.6 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

1. Draw a map of the game and how you flow through it.

2. Fix all of your mistakes, including spelling mistakes.

3. Write comments for the functions you do not understand.

4. Add more to the game. What can you do to both simplify and expand it?

5. 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 what I’ve written? Look at how int() works for clues.

Common Student Questions

Help! How does this program work?! When you get stuck understanding a piece of code, simply write an English comment above every line explaining what that line does. Keep your comments short and similar to the code. Then either diagram how the code works or write a paragraph describing it. If you do that you’ll get it.

Why did you write 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).

Why is input() sometimes written as input('> ')? The parameter to input is a string that it should print as a prompt before getting the user’s input.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.97.61