Skip to content

Commit 16bea3d

Browse files
committed
完成第八章函数的课后练习
1 parent 23fbc04 commit 16bea3d

File tree

11 files changed

+171
-0
lines changed

11 files changed

+171
-0
lines changed

chapter08/8-10.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def show_magicians(names):
2+
for name in names:
3+
print(name)
4+
5+
def make_great(names):
6+
great_magicians = []
7+
while names:
8+
magician = names.pop()
9+
great_magician = magician + ' the Great'
10+
great_magicians.append(great_magician)
11+
12+
for great_magician in great_magicians:
13+
names.append(great_magician)
14+
15+
16+
magicians = ['znn','david','amy']
17+
show_magicians(magicians)
18+
19+
print("\n")
20+
make_great(magicians)
21+
show_magicians(magicians)

chapter08/8-11.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def show_magicians(names):
2+
for name in names:
3+
print(name)
4+
5+
def make_great(names):
6+
great_magicians = []
7+
while names:
8+
magician = names.pop()
9+
great_magician = magician + ' the Great'
10+
great_magicians.append(great_magician)
11+
12+
for great_magician in great_magicians:
13+
names.append(great_magician)
14+
15+
return names
16+
17+
18+
magicians = ['znn','david','amy']
19+
show_magicians(magicians)
20+
21+
print("\nGreat magicians:")
22+
great_magicians = make_great(magicians[:])
23+
show_magicians(great_magicians)
24+
25+
print("\nOriginal magicians:")
26+
show_magicians(magicians)

chapter08/8-12.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# encoding: utf-8
2+
3+
# 切记*items这不是指针,是创建一个名为items的空元组,并将收到的所有值都封装到这个元组中
4+
def make_sandwich(*items):
5+
for item in items:
6+
print("...adding " + item)
7+
8+
make_sandwich('roast beef', 'cheddar cheese', 'lettuce', 'honey dijon')
9+
make_sandwich('turkey', 'apple slices', 'honey mustard')
10+
make_sandwich('peanut butter', 'strawberry jam')

chapter08/8-13.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def build_profile(first, last, **user_info):
2+
profile = {}
3+
profile['first_name'] = first
4+
profile['last_name'] = last
5+
for key, value in user_info.items():
6+
profile[key] = value
7+
return profile
8+
9+
user_profile = build_profile('albert', 'einstein',
10+
location='princeton',
11+
field='physics')
12+
print(user_profile)

chapter08/8-14.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def make_car(manufacturer, model, **options):
2+
car_dict = {
3+
'manufacturer': manufacturer.title(),
4+
'model': model.title(),
5+
}
6+
for option, value in options.items():
7+
car_dict[option] = value
8+
9+
return car_dict
10+
11+
my_outback = make_car('subaru', 'outback', color='blue', tow_package=True)
12+
print(my_outback)
13+
14+
my_accord = make_car('honda', 'accord', year=1991, color='white',
15+
headlights='popup')
16+
print(my_accord)

chapter08/8-6.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def city_country(city,country):
2+
return (city.title() + ',' + country.title())
3+
4+
city = city_country('shanghai', 'china')
5+
print(city)
6+
7+
city = city_country('ushuaia', 'argentina')
8+
print(city)
9+
10+
city = city_country('longyearbyen', 'svalbard')
11+
print(city)

chapter08/8-7.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def make_album(artist, title):
2+
album_dict = {
3+
'artist': artist.title(),
4+
'title': title.title(),
5+
}
6+
return album_dict
7+
8+
album = make_album('metallica', 'ride the lightning')
9+
print(album)
10+
11+
album = make_album('beethoven', 'ninth symphony')
12+
print(album)
13+
14+
album = make_album('willie nelson', 'red-headed stranger')
15+
print(album)

chapter08/8-8.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def make_album(artist, title, tracks=0):
2+
"""Build a dictionary containing information about an album."""
3+
album_dict = {
4+
'artist': artist.title(),
5+
'title': title.title(),
6+
}
7+
if tracks:
8+
album_dict['tracks'] = tracks
9+
return album_dict
10+
11+
title_prompt = "\nWhat album are you thinking of? "
12+
artist_prompt = "Who's the artist? "
13+
14+
print("Enter 'quit' at any time to stop.")
15+
16+
while True:
17+
title = raw_input(title_prompt)
18+
if title == 'quit':
19+
break
20+
21+
artist = raw_input(artist_prompt)
22+
if artist == 'quit':
23+
break
24+
25+
album = make_album(artist, title)
26+
print(album)
27+
28+
print("\nThanks for responding!")

chapter08/8-9.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def show_magicians(names):
2+
for name in names:
3+
print(name)
4+
5+
magicians = ['znn','david','amy']
6+
show_magicians(magicians)

chapter08/print_models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import printing_functions
2+
3+
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
4+
completed_models = []
5+
6+
print_models(unprinted_designs, completed_models)
7+
show_completed_models(completed_models)

chapter08/printing_functions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+

0 commit comments

Comments
 (0)