Skip to content

Commit 322df65

Browse files
committed
Sample code to understand branches and functions in python
1 parent b693e47 commit 322df65

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

samplegame.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# SAMPLE CODE TO ILLUTRATE THE CONCEPT OF BRANCHES AND FUNCTIONS IN PYTHON
2+
# Users are advised to go through this piece of code and dry run it to get an understanding of function calls.
3+
4+
from sys import exit
5+
def gold_room():
6+
print "This room is full of gold.How much do you take?"
7+
next = raw_input("> ")
8+
if "0" in next or "1" in next:
9+
how_much = int(next)
10+
else:
11+
dead("Man, learn to type a number.")
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+
def bear_room():
18+
print "There is a bear here."
19+
print "The bear has a bunch of honey."
20+
print "The fat bear is in front of another door."
21+
print "How are you going to move the bear?"
22+
bear_moved = False
23+
while True:
24+
next = raw_input("> ")
25+
if next == "take honey":
26+
dead("The bear looks at you then slaps your face off.")
27+
elif next == "taunt bear" and not bear_moved:
28+
print "The bear has moved from the door. You can go through it now."
29+
bear_moved = True
30+
elif next == "taunt bear" and bear_moved:
31+
dead("The bear gets pissed off and chews your leg off.")
32+
elif next == "open door" and bear_moved:
33+
gold_room()
34+
else:
35+
print "I got no idea what that means."
36+
37+
def cthulu_room():
38+
print "Here you see the great evil Cthulu."
39+
print "He, it, whatever stares at you and you go insane."
40+
print "Do you flee for your life or eat your head?"
41+
next = raw_input("> ")
42+
if "flee" in next:
43+
start()
44+
elif "head" in next:
45+
dead("Well that was tasty!")
46+
else:
47+
cthulu_room()
48+
49+
def dead(why):
50+
print why, "Good job!"
51+
exit(0)
52+
53+
def start():
54+
print "You are in a dark room."
55+
print "There is a door to your right and left."
56+
print "Which one do you take?"
57+
next = raw_input("> ")
58+
if next == "left":
59+
bear_room()
60+
elif next == "right":
61+
cthulu_room()
62+
else:
63+
dead("You stumble around the room until you starve.")
64+
65+
start()
66+

0 commit comments

Comments
 (0)