Skip to content

Commit c50257b

Browse files
committed
Added code from chapter 8.
1 parent d79af17 commit c50257b

8 files changed

+94
-0
lines changed

chapter_08/formatted_name.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def get_formatted_name(first_name, last_name, middle_name=''):
2+
"""Return a full name, neatly formatted."""
3+
if middle_name:
4+
full_name = first_name + ' ' + middle_name + ' ' + last_name
5+
else:
6+
full_name = first_name + ' ' + last_name
7+
return full_name.title()
8+
9+
musician = get_formatted_name('jimi', 'hendrix')
10+
print(musician)
11+
12+
musician = get_formatted_name('john', 'hooker', 'lee')
13+
print(musician)

chapter_08/greet_users.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def greet_users(names):
2+
"""Print a simple greeting to each user in the list."""
3+
for name in names:
4+
msg = "Hello, " + name.title() + "!"
5+
print(msg)
6+
7+
usernames = ['hannah', 'ty', 'margot']
8+
greet_users(usernames)

chapter_08/greeter.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def greet_user(username):
2+
"""Display a simple greeting."""
3+
print("Hello, " + username.title() + "!")
4+
5+
greet_user('jesse')

chapter_08/person.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def build_person(first_name, last_name, age=''):
2+
"""Return a dictionary of information about a person."""
3+
person = {'first': first_name, 'last': last_name}
4+
if age:
5+
person['age'] = age
6+
return person
7+
8+
musician = build_person('jimi', 'hendrix', age=27)
9+
print(musician)

chapter_08/pets.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def describe_pet(pet_name, animal_type='dog'):
2+
"""Display information about a pet."""
3+
print("\nI have a " + animal_type + ".")
4+
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
5+
6+
# A dog named Willie.
7+
describe_pet('willie')
8+
describe_pet(pet_name='willie')
9+
10+
# A hamster named Harry.
11+
describe_pet('harry', 'hamster')
12+
describe_pet(pet_name='harry', animal_type='hamster')
13+
describe_pet(animal_type='hamster', pet_name='harry')

chapter_08/pizza.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def make_pizza(size, *toppings):
2+
"""Summarize the pizza we are about to make."""
3+
print("\nMaking a " + str(size) +
4+
"-inch pizza with the following toppings:")
5+
for topping in toppings:
6+
print("- " + topping)
7+
8+
make_pizza(16, 'pepperoni')
9+
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

chapter_08/printing_models.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def print_models(unprinted_designs, completed_models):
2+
"""
3+
Simulate printing each design, until there are none left.
4+
Move each design to completed_models after printing.
5+
"""
6+
while unprinted_designs:
7+
current_design = unprinted_designs.pop()
8+
9+
# Simulate creating a 3d print from the design.
10+
print("Printing model: " + current_design)
11+
completed_models.append(current_design)
12+
13+
def show_completed_models(completed_models):
14+
"""Show all the models that were printed."""
15+
print("\nThe following models have been printed:")
16+
for completed_model in completed_models:
17+
print(completed_model)
18+
19+
20+
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
21+
completed_models = []
22+
23+
print_models(unprinted_designs, completed_models)
24+
show_completed_models(completed_models)

chapter_08/user_profile.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def build_profile(first, last, **user_info):
2+
"""Build a dictionary containing everything we know about a user."""
3+
profile = {}
4+
profile['first_name'] = first
5+
profile['last_name'] = last
6+
for key, value in user_info.items():
7+
profile[key] = value
8+
return profile
9+
10+
user_profile = build_profile('albert', 'einstein',
11+
location='princeton',
12+
field='physics')
13+
print(user_profile)

0 commit comments

Comments
 (0)