Skip to content

Commit 9f5511e

Browse files
Sean HincheeSean Hinchee
Sean Hinchee
authored and
Sean Hinchee
committed
Adding all original exercise programs from Jacob O directly. Thanks Jacob.
1 parent 21d235e commit 9f5511e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1291
-1
lines changed

2python/Life as a dwarf.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Game loss - woops you lost
2+
3+
Game win - hurray you win
4+
5+
Well - start of game, must swim down
6+
7+
Cavern - come out of river, see bat
8+
9+
Tunnel - escape is near
10+

2python/ex1.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
print "I decided to redo all these exercies"
2+
print "Because i dont really understand any of them"
3+
print "So this is me doing that."
4+
print "yeah"
5+
print "in other news, 400 prisoners"
6+
print "will soon be building a fortress of dwarves"
7+
print "but not today"

2python/ex2.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jk nothing here

2python/ex3.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
print "wee math"
2+
print 25 + 30 / 6
3+
print 100 - 24 * 3 % 4
4+
print "wee math"
5+
print 3 + 2 + 1 + 4 + 5 + 6 + 7
6+
print 3 + 2 < 5 - 7
7+
print 3 + 2
8+
print 5 - 8
9+
print 5 > -2
10+
print 5 >= -2
11+
print 4 <= -2

2python/ex4.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
a = 100
2+
b = 4.0
3+
c = 30
4+
d = 90
5+
e = a-c
6+
f = c
7+
h = f * b
8+
i = d / f
9+
10+
11+
print "there are", a, "a's"
12+
print "there are only", c, "c's"
13+
print "there will be", e, "e's"
14+
print "we can move", h, "d's"
15+
print "we have", d, "to move"
16+
print "we need", i, "per a"

2python/ex5.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name = "Nathan"
2+
age = 17
3+
height = 23
4+
faget = 23
5+
eyes = "yuck"
6+
teeth = "yellow"
7+
hair = "none"
8+
9+
print "let's talk about %s" % name
10+
print "he's %d tall" % height
11+
print "he's % fagets" % faget
12+
print "he's got %s eyes and %s hair" % (eyes, hair)
13+
print "he has %s teeth" % teeth
14+
15+
16+
print "if i add %d, %d, and %d i get %d" % (
17+
age, height, faget, age + height + faget)

2python/ex6.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
x = "there are %d people" % 10
2+
binary = 'binary'
3+
do_not = 'don\'t'
4+
y = 'those who know %s and those who %s' % (binary, do_not)
5+
6+
print x
7+
print y
8+
9+
print '%r' % x
10+
print '%s' % y
11+
12+
funny = False
13+
joke = 'funny? %r'
14+
15+
print joke % funny
16+
17+
w = 'this is the left'
18+
e = 'this is the right'
19+
20+
print w + e

2python/untitled text 2.txt

Whitespace-only changes.

Lifeasadwarf.py

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
from sys import exit
2+
from random import randint
3+
4+
5+
class Scene(object):
6+
7+
def enter(self):
8+
print "This scene is not configured yet. Subclass it and implement enter()"
9+
exit(1)
10+
11+
12+
class Engine(object):
13+
14+
def __init__(self, scene_map):
15+
self.scene_map = scene_map
16+
17+
def play(self):
18+
current_scene = self.scene_map.opening_scene()
19+
last_scene = self.scene_map.next_scene('finished')
20+
21+
while current_scene != last_scene:
22+
next_scene_name = current_scene.enter()
23+
current_scene = self.scene_map.next_scene(next_scene_name)
24+
25+
current_scene.enter()
26+
27+
class Loss(Scene):
28+
29+
quips = [
30+
"Woops you lose."
31+
32+
]
33+
34+
def enter(self):
35+
print Loss.quips[randint(0, len(self.quips)-1)]
36+
exit(1)
37+
38+
class Well(Scene):
39+
40+
def enter(self):
41+
42+
print "You are a dwarf from Argentina. You live in Nathan's"
43+
print "lovely fortress. Your assigned duties for today are"
44+
print "hauling water from the local well. As you bring up"
45+
print "the bucket, you slip and fall into the well!"
46+
print "\n"
47+
print "You're now stuck in a well. You have to get out or"
48+
print "starve. Do you want to: 1. yell for help, 2. try to"
49+
print "climb out, or 3. look for an exit."
50+
51+
action = raw_input("> ")
52+
53+
if action == "1":
54+
print "You decide to put your vocal cords to the test"
55+
print "and obnoxiously sing Darude Sandstorm. After"
56+
print "the third repition, no one has come. You start"
57+
print "yelling more desperately, but no one heres you."
58+
return 'Loss'
59+
60+
elif action == "2":
61+
print "All those years at the gym will surely pay off."
62+
print "You latch onto the wall and begin to hoist yourself"
63+
print "out. The start is very slippery and you end up having"
64+
print "to restart twice. On your third attempt you make it to"
65+
print "the top. You poke your head out only to be smacked by"
66+
print "your buddy who's come looking for you. You fall back"
67+
print "down the well."
68+
return 'Loss'
69+
70+
elif action == "3":
71+
print "You decide to take a look around while you're stuck down"
72+
print "here. After a brief glance, you don't see anything."
73+
print "You keep looking and you see a small glow from a crevice"
74+
print "in the wall under the water. You swim towards the light."
75+
return 'Cavern'
76+
77+
else:
78+
print "Please enter a number."
79+
return 'Well'
80+
81+
class Cavern(Scene):
82+
83+
def enter(self):
84+
print "The light leads you to an underground cavern. Looking up,"
85+
print "you can just barely see the roof. The natural light seems"
86+
print "to be coming from some mushrooms scattered around. Do you"
87+
print "want to: 1. explore the cavern, 2. go back the way you came"
88+
print "3 sit and wait."
89+
90+
action = raw_input ("> ")
91+
92+
if action == "1":
93+
print "Time to keep using the look command. A quick glance reveals"
94+
print "an entrance to a tunnel across the cavern. Rather than sitting"
95+
print "around all day, you decide to go to the tunnel. On the way you"
96+
print "pass some skeletons."
97+
return 'Tunnel'
98+
99+
elif action == "2":
100+
print "This cave is scary. You go back to the well."
101+
return 'Well'
102+
103+
elif action == "3":
104+
print "You're really tired. Like, really, really tired. You sit down."
105+
print "A couple of years later some lost dwarves find your skeleton."
106+
return 'Loss'
107+
108+
else:
109+
print "Please enter a number."
110+
return 'Cavern'
111+
112+
113+
class Tunnel(Scene):
114+
115+
def enter(self):
116+
print "You enter a tunnel. There are three ways to go. Do you go 1. Left,"
117+
print "2. Right, or 3. Back?"
118+
119+
correct_way = "%d" % (randint(1,2))
120+
121+
action = raw_input("> ")
122+
123+
if action == "3":
124+
print "This tunnel is 2sketchy4you. You go back to the cavern"
125+
return 'Cavern'
126+
127+
elif action == correct_way:
128+
print "You start walking. After maybe 10 minutes the tunnel curves."
129+
print "When you get around the bend, you see sunlight."
130+
print "\n"
131+
print "Congratulations, you win!"
132+
quit(1)
133+
134+
else:
135+
print "You get bored of thinking and start walking. After maybe 10"
136+
print "minutes the tunnel curvese. When you get around the bend,"
137+
print "you see sunlight! There's also a bear... Uh oh."
138+
return 'Loss'
139+
140+
141+
class Map(object):
142+
143+
scenes = {
144+
'Well': Well(),
145+
'Cavern':Cavern(),
146+
'Tunnel':Tunnel(),
147+
'Loss':Loss(),
148+
}
149+
150+
def __init__(self, start_scene):
151+
self.start_scene = start_scene
152+
153+
def next_scene(self, scene_name):
154+
val = Map.scenes.get(scene_name)
155+
return val
156+
157+
def opening_scene(self):
158+
return self.next_scene(self.start_scene)
159+
160+
161+
a_map = Map('Well')
162+
a_game = Engine(a_map)
163+
a_game.play()

Python Characters.rtf

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
2+
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
3+
{\colortbl;\red255\green255\blue255;}
4+
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
5+
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
6+
7+
\f0\fs24 \cf0 print - Displays anything enclosed in " " \
8+
# - Commenting system, Python ignores everything after it\
9+
% - Call sign for a variable\
10+
%s - "Replace this with a string"\
11+
%r - Used for debuggin\
12+
%d - Decimal\
13+
%c - Single character\
14+
= - equals\
15+
- - subtract\
16+
+ - add\
17+
* - multiple\
18+
/ - divide\
19+
\\n - new line \
20+
\\t - Tab (indent)\
21+
""" (3 double quotes) - everything following is a string until it is closed \
22+
raw_input - uses Input from outside source\
23+
from - location of item/object being used\
24+
import - bring in outside source \
25+
def - define; used to set strings with words\
26+
\
27+
}

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
# Python-Exercises
2-
A collection of solutions to the "Learn Python the Hard Way" exercises
2+
A collection of solutions to the "Learn Python the Hard Way" exercises.
3+
4+
Almost all, if not all, programs in this repository were originally conceived or composed by Jacob O.
5+
6+
Thank you Jacob O for the valuable contribution.
7+
8+

ex1.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
print "I'm typing stuff"
2+
print "I'm doing it again"
3+
print "This is very strange"
4+
print "I spy an altoids can
5+
print "I type print a lot"
6+
print "Such print very wow"
7+
print "Doge"

ex10.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tabby_cat = "\tI'm tabbed in."
2+
persian_cat = "I'm split\non a line"
3+
backslash_cat = "I'm \\ a \\ cat"
4+
5+
fat_cat = """
6+
I'll do a list:
7+
\t* Cat food
8+
\t* Fishies
9+
\t* Catnip\n\t* Grass
10+
"""
11+
12+
print tabby_cat
13+
print persian_cat
14+
print backslash_cat
15+
print fat_cat

ex11.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
print "How old are you?",
2+
age = raw_input ()
3+
print "How tall are you?",
4+
height = raw_input ()
5+
print "How much do you weigh?",
6+
weight = raw_input ()
7+
8+
print "So, you're %r old, %r tall, and %r heavy." % (
9+
age, height, weight)

ex12.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
age = raw_input ("How old are you? ")
2+
height = raw_input ("How tall are you? ")
3+
weight = raw_input ("How much do you weigh? ")
4+
5+
print "So you're %r old, %r tall, and %r heavy." % (
6+
age, height, weight)

ex13.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from sys import argv # Import brings in outside information
2+
# and helps compress the code (modules)
3+
script, first, second, third = argv
4+
# Line 3 "unpacks" argv. It assigns it variables to use
5+
print "The script is called:", script
6+
print "Your first variable is:", first
7+
print "Your second variable is:", second
8+
print "Your third variable is:", third

ex14.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from sys import argv
2+
3+
script, user_name = argv
4+
prompt = '> '
5+
6+
print "Hi %s, I'm the %s script." % (user_name, script)
7+
print "I'd like to ask you a few questions."
8+
print "Are you hungry %s?" % user_name
9+
hungry = raw_input (prompt)
10+
11+
print "Where do you live %s?" % user_name
12+
lives = raw_input (prompt)
13+
14+
print "What kind of computer do you have?"
15+
computer = raw_input (prompt)
16+
17+
print """
18+
Alright, so you said %r about being hungry.
19+
You live in %r. No idea where that is.
20+
And you have a %r computer. Acceptable.
21+
""" % (hungry, lives, computer)

0 commit comments

Comments
 (0)