#8-1 消息
def display_message():
print("你学了什么?")
display_message()
#8-2 喜欢的图书
def favorite_book(book):
print("one of my favorite books is %s"%book)
favorite_book('ni')
#8-3 T恤
#位置调参
def make_shirt(size,words):
print(f"\nthe t-shirt size is {size},the words is {words}.")
make_shirt('l','love')
#关键字调参
def make_shirt(size,words):
print(f"the t-shirt size is {size},the words is {words}.")
make_shirt(size='m',words='china')
#8-4 大号T恤
def make_lshirt(size,words='i love python'):
print(f"the size is {size},the words is {words}.")
make_lshirt(size='l')
make_lshirt(size='m')
make_lshirt(size='s',words='i love u')
#8-5 城市
def describe_city(city,country='china'):
print(f"{city} is in {country}.")
describe_city(city='beijing')
describe_city('london','UK')
#8-6 城市名
def city_country(city,country):
return city +','+country
print(city_country('hangzhou','china'))
print(city_country('xian','china'))
#8-7 专辑
def make_album(name,cd_name,cd_num = ''):
album = {'歌手':name.title(),'专辑':cd_name.title()}
if cd_num:
album['歌曲数量'] = cd_num
return album
value = make_album('michael jackson','billie jean')
print(value)
value = make_album('周杰伦','双截棍')
print(value)
value = make_album('张学友','smile','11')
print(value)
#8-8 用户的专辑
def make_album(name,cd_name):
album = {'歌手':name.title(),'专辑':cd_name.title()}
return album
while True:
print('\n你对你喜欢的歌手有多了解?')
print('我们来做个调查,关闭调查输入‘Q’')
name = input('\n输入你喜欢的歌手名字')
if name == 'Q':
break
cd_name = input('\n输入你知道的专辑名字')
if cd_name == 'Q':
break
album_1 = make_album(name,cd_name)
print('\n你喜欢的歌手信息如下,你觉得对吗?\n' + str(album_1))
#8-9 消息
def show_message(books):
for book in books:
message=f"to read book:{book.title()}"
print(message)
read_books=['python','java']
show_message(read_books)
#8-10 发送消息
def show_messages(unread_books,read_books):
while unread_books:
toread_books=unread_books.pop()
print(f"to read books {toread_books}")
read_books.append(toread_books)
def send_messages(read_books):
print('\n:These books have been read :')
for book in read_books:
print(book)
unread_books = ['python','java','c']
read_books = []
show_messages(unread_books,read_books)
send_messages(read_books)
#8-12 三明治
def sandwiches(*toppings):
print("your sandwiches toppings:")
for topping in toppings:
print(f"{topping}")
sandwiches('tomato')
sandwiches('beef','meat')
《Python编程从入门到实践》练习题代码实现第八章
最新推荐文章于 2022-10-20 18:59:36 发布