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:

Listing 35.1: 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." ...

Get Learn Python the Hard Way: A Deceptively Simple Introduction to the Terrifyingly Beautiful World of Computers and Data Science, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.