8-1
def display_message():
print("study function")
display_message()
8-2
def favorite_book(title):
print(f"One of my favorite books is {title.title()}")
book = input('whatis your favorite book?')
favorite_book(book)
8-3
def make_shirt(size, word):
print(f"This t-shirt is {size} and is printed with {word} characters")
word = input("it is printed your shirt:")
size = input("size:")
make_shirt(size, word)
8-4
#打印 I Love python
def make_shirt(size, word='I love python'):
print(f"This t-shirt is {size} and is printed with {word} characters")
make_shirt(size='10')
#打印其他语句
def make_shirt(size, word='I love python'):
print(f"This t-shirt is {size} and is printed with {word} characters")
make_shirt(size='10',word='I love C++')
8-5
def describe_city(name, country='china'):
print(f"{name.title()} is in {country.title()}")
describe_city('beijing')
describe_city('tokyo', 'japan')
describe_city(name='Washington', country='america')
8-6
def city_country(name, country):
country_name = f"{name} , {country}"
return country_name.title()
name1 = city_country('beijing', 'china')
name2 = city_country('tokyo', 'japan')
name3 = city_country(name='Washington', country='america')
print(name1)
print(name2)
print(name3)
8-7
#使用国家代替专辑
def make_album(name, album, num=''):
album_name = {'name': name, 'album_name': album}
if num:
album_name['number'] = num
return album_name
name1 = make_album('beijing', 'china',5)
name2 = make_album('tokyo', 'japan',10)
name3 = make_album(name='Washington', album='america')
print(name1)
print(name2)
print(name3)
8-8
def make_album(name, album, number=''):
album_name = {'name': name, 'album_name': album}
if number:
album_name['number'] = number
return album_name
flag = True
while flag:
name = input("Input singer's name:")
album = input("Input album's name:")
m = make_album(name, album)
print(m)
leave = input("Do you input enough?(y/n)")
if leave.upper() == 'Y':
break
8-9
def pri_text(texts):
for text in texts:
print(text)
texts = ['show', 'person', 'country', 'day']
pri_text(texts)
8-10
def send_messages(send_message, sent_message ):
while send_message:
m = send_message.pop()
sent_message.append(m)
print(f"{send_message}")
print(f"{sent_message}")
send_message = ['show', 'person', 'country', 'day']
sent_message = []
send_messages(send_message, sent_message)
8-11
def send_messages(send_message, sent_message):
while send_message:
m = send_message.pop()
sent_message.append(m)
print(f"{send_message}")
print(f"{sent_message}")
send_message = ['show', 'person', 'country', 'day']
sent_message = []
send_messages(send_message[:], sent_message[:])
print(f"{send_message}")
print(f"{sent_message}")
#结果
[]
['day', 'country', 'person', 'show']
['show', 'person', 'country', 'day']
[]
8-12
def make_pizza(*toppings):
print("\nMaking a pizza with the following toppings:")
for topping in toppings:
print(f"- {topping}")
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
#输出
Making a pizza with the following toppings:
- pepperoni
Making a pizza with the following toppings:
- mushrooms
- green peppers
Making a pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
8-13
def build_profile(first, last, **user_info):
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('alice', 'harden',
location='xian',
filed='physics')
print(user_profile)
#输出
{'location': 'xian', 'filed': 'physics', 'first_name': 'alice', 'last_name': 'harden'}
8-14
def make_car(manufacturer, model, **user_info):
user_info['manufacturer'] = manufacturer
user_info['model'] = model
return user_info
car = make_car('subaru', 'outback', color='blue', two_package=True)
print(car)
#输出
{'color': 'blue', 'two_package': True, 'manufacturer': 'subaru', 'model': 'outback'}