Skip to content

Commit fe33742

Browse files
author
Beej Jorgensen
committed
add examples
1 parent 62e51ef commit fe33742

14 files changed

Lines changed: 394 additions & 2 deletions

File tree

‎.vscode/settings.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "/Users/beej/.local/share/virtualenvs/djorg-beej-fCywqo0Z/bin/python"
3+
}

‎Pipfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ name = "pypi"
66
[packages]
77

88
[dev-packages]
9+
pylint = "*"
910

1011
[requires]
1112
python_version = "3"

‎Pipfile.lock‎

Lines changed: 80 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/adv.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Write a text adventure that allows the player to move from room to room by
2+
# typing "n", "w", "s", or "e" for north, west, south, and east.
3+
4+
import textwrap
5+
6+
# These are the existing rooms. Add more as you see fit.
7+
8+
rooms = {
9+
"outside": {
10+
"name": "Outside Cave Entrance",
11+
"description": "North of you, the cave mouth beckons.",
12+
"n_to": "foyer",
13+
},
14+
15+
"foyer": {
16+
"name": "Foyer",
17+
"description": "Dim light filters in from the south. Dusty passages run north and east.",
18+
"n_to": "overlook",
19+
"s_to": "outside",
20+
"e_to": "narrow",
21+
},
22+
23+
"overlook": {
24+
"name": "Grand Overlook",
25+
"description": """A steep cliff appears before you, falling
26+
into the darkness. Ahead to the north, a light flickers in
27+
the distance, but there is no way across the chasm.""",
28+
"s_to": "foyer",
29+
},
30+
31+
"narrow": {
32+
"name": "Narrow Passage",
33+
"description": "The narrow passage bends here from west to north. The smell of gold permeates the air.",
34+
"w_to": "foyer",
35+
"n_to": "treasure",
36+
},
37+
38+
"treasure": {
39+
"name": "Treasure Chamber",
40+
"description": """You've found the long-lost treasure
41+
chamber. Sadly, it has already been completely emptied by
42+
earlier adventurers. The only exit is to the south.""",
43+
"s_to": "narrow",
44+
},
45+
46+
}
47+
48+
""" template room to copy into code
49+
"room": {
50+
"name": "",
51+
"description": "",
52+
"n_to": "",
53+
"s_to": "",
54+
"e_to": "",
55+
"w_to": "",
56+
},
57+
"""
58+
59+
# Write a class to hold player information, e.g. what room they are in currently
60+
class Player:
61+
"""Holds information about a player"""
62+
def __init__(self, startRoom):
63+
self.curRoom = startRoom
64+
65+
def tryDirection(d, curRoom):
66+
"""
67+
Try to move a direction, or print an error if the player can't go that way.
68+
69+
Returns the room the player has moved to (or the same room if the player didn't move).
70+
"""
71+
key = d + "_to"
72+
73+
if key not in rooms[curRoom]:
74+
print("You can't go that way")
75+
return curRoom
76+
77+
dest = rooms[curRoom][key]
78+
79+
return dest
80+
81+
82+
#
83+
# Main
84+
#
85+
86+
# Make a new player object that is currently in the 'outside' room.
87+
88+
p = Player('outside')
89+
90+
# Write a loop that:
91+
#
92+
# * Prints the current room name
93+
# * Prints the current description (the textwrap module might be useful here).
94+
# * Waits for user input and decides what to do.
95+
#
96+
# If the user enters a cardinal direction, attempt to move to the room there.
97+
# Print an error message if the movement isn't allowed.
98+
#
99+
# If the user enters "q", quit the game.
100+
101+
done = False
102+
103+
while not done:
104+
# Print the room name
105+
print("\n{}\n".format(rooms[p.curRoom]['name']))
106+
107+
# Print the room description
108+
for line in textwrap.wrap(rooms[p.curRoom]['description']):
109+
print(line)
110+
111+
# User prompt
112+
s = input("\nCommand> ").strip().lower()
113+
114+
# Handle input
115+
if s == "q":
116+
done = True
117+
elif s in ["n", "s", "w", "e"]:
118+
p.curRoom = tryDirection(s, p.curRoom)
119+
else:
120+
print("Unknown command {}".format(s))
121+

‎src/bignum.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Print out 2 to the 65536 power
2+
3+
print(2**65536)

‎src/cal.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Use the 'calendar' module to draw calendars to the console
2+
# https://docs.python.org/3.6/library/calendar.html
3+
#
4+
# Use the sys module to look for command line arguments in the `argv` list
5+
# variable.
6+
#
7+
# If the user specifies two command line arguments, month and year, then draw
8+
# the calendar for that month.
9+
10+
# Stretch goal: if the user doesn't specify anything on the command line, show
11+
# the calendar for the current month. See the 'datetime' module.
12+
13+
# Hint: this should be about 15 lines of code. No loops are required. Read the
14+
# docs for the calendar module closely.
15+
16+
import sys
17+
import calendar
18+
19+
# Parse command line
20+
l = len(sys.argv)
21+
22+
if l == 2:
23+
month = None
24+
year = int(sys.argv[1])
25+
elif l == 3:
26+
month = int(sys.argv[1])
27+
year = int(sys.argv[2])
28+
else:
29+
print("usage: cal.py [month] year")
30+
sys.exit(1)
31+
32+
# Make a new calendar
33+
c = calendar.TextCalendar()
34+
35+
if month != None:
36+
# If the user specified a month, print that month
37+
c.prmonth(year, month)
38+
else:
39+
# Otherwise just print it for the year
40+
c.pryear(year)

‎src/comp.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Use a list comprehension to create a list containing only the _even_ elements
2+
# the user entered into list x.
3+
4+
x = input("Enter comma-separated numbers: ").split(',')
5+
6+
# What do you need between the square brackets to make it work?
7+
y = [i for i in x if int(i) % 2 == 0]
8+
9+
print(y)
10+

‎src/datatypes.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
x = 5
2+
y = "7"
3+
4+
# Write a print statement that combines x + y into the integer value 12
5+
print(x + int(y))
6+
7+
# Write a print statement that combines x + y into the string value 57
8+
print(str(x) + y)

‎src/dicts.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Make an array of dictionaries. Each dictionary should have keys:
2+
#
3+
# lat: the latitude
4+
# lon: the longitude
5+
# name: the waypoint name
6+
#
7+
# Make up three entries of various values.
8+
9+
waypoints = [
10+
{
11+
"lat": 43,
12+
"lon": -121,
13+
"name": "a place"
14+
},
15+
{
16+
"lat": 41,
17+
"lon": -123,
18+
"name": "another place"
19+
},
20+
{
21+
"lat": 43,
22+
"lon": -122,
23+
"name": "a third place"
24+
}
25+
]
26+
27+
# Write a loop that prints out all the field values for all the waypoints
28+
for w in waypoints:
29+
print(w['name'], w['lat'], w['lon'])

‎src/func.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Write a function is_even that will return true if the passed in number is even.
2+
def is_even(x):
3+
return (x % 2) == 0
4+
5+
# Read a number from the keyboard
6+
num = input("Enter a number: ")
7+
8+
# Print out "Even!" if the number is even. Otherwise print "Odd"
9+
num = int(num)
10+
11+
if is_even(num):
12+
print("Even!")
13+
else:
14+
print("Odd")

0 commit comments

Comments
 (0)