《Python编程从入门到实践》第8章习题

8-1

def display_message(profession):
    print('我的大学专业是'+profession)

display_message('电科')
我的大学专业是电科

8-2

def favorite_book(title):
    print('One of my favorite books is '+title+'.')

favorite_book('Flutter')
One of my favorite books is Flutter.

8-3

def make_shirt(size,pattern):
    print('The size is '+size+'.')
    print('The pattern is '+pattern+'.')

make_shirt('XL','China')
The size is XL.
The pattern is China.

8-4

def make_shirt(size,pattern='I love Python'):
    print('The size is "'+size+'".')
    print('The pattern is "'+pattern+'".')

make_shirt('big')
make_shirt('medium')
make_shirt('big','Chine')
The size is "big".
The pattern is "I love Python".
The size is "medium".
The pattern is "I love Python".
The size is "big".
The pattern is "Chine".

8-5

def describe_city(city,country='US'):
    print(city.title()+' is in '+country+'.')

describe_city('London','UK')
describe_city('New York')
describe_city('Los Angeles')
London is in UK.
New York is in US.
Los Angeles is in US.

8-6

def city_country(city,country):
    return (city+','+country)

print(city_country('London','UK'))
print(city_country('New York','US'))
print(city_country('Los Angeles','US'))
London,UK
New York,US
Los Angeles,US

8-7

def make_album(name,album,number=' '):
    albums={}
    albums[name]=album
    if number != ' ':
        albums['number']=number
    return albums

print(make_album('A','a'))
print(make_album('B','b','5'))
print(make_album('C','c'))
{'A': 'a'}
{'B': 'b', 'number': '5'}
{'C': 'c'}

8-8

def make_album(name,album,number=' '):
    albums={}
    albums[name]=album
    if number != ' ':
        albums['number']=number
    return albums

while True:
    name=input('请输入歌手名:')
    if name != 'quit':
        album=input('请输入专辑名:')
        print(make_album(name,album))
    else:
        break
请输入歌手名:A
请输入专辑名:a
{'A': 'a'}
请输入歌手名:quit

8-9

names=['A','B','C']
def show_magicians(names):
    for name in names:
        print(name)

show_magicians(names)
A
B
C

8-10

names=['A','B','C']
new_names=[]
def make_great(names):
    for name in names:
        name='The Great '+name
        new_names.append(name)
    return new_names

def show_magicians(new_names):
    for name in new_names:
        print(name)

a=make_great(names)
show_magicians(a)
The Great A
The Great B
The Great C

8-11

names=['A','B','C']
new_names=[]
def make_great(names):
    for name in names:
        name='The Great '+name
        new_names.append(name)
    return new_names

def show_magicians(names):
    for name in names:
        print(name)

a=make_great(names[:])
show_magicians(a)
show_magicians(names)
The Great A
The Great B
The Great C
A
B
C

8-12

def make_sandwich(*food_material):
    print(food_material)

make_sandwich('salt')
make_sandwich('egg','bread')
make_sandwich('egg','lettuce','salt')
('salt',)
('egg', 'bread')
('egg', 'lettuce', 'salt')

8-13

def build_profile(first,last,**user_info):
    '''创建一个字典,其中包含我们知道的有关用户的一切'''
    profile={}
    profile['first_name']=first
    profile['last_name']=last
    for key,value in user_info.items():
        profile[key]=value
    return profile

user_profile=build_profile('张','三',身高='180cm',体重='70kg')
print(user_profile)
{'first_name': '张', 'last_name': '三', '身高': '180cm', '体重': '70kg'}

8-14

def car_profile(manufacturer,model,**message):
    profile={}
    profile['manufacturer']=manufacturer
    profile['model']=model
    for key,value in message.items():
        profile[key]=value
    return profile

car=car_profile('subaru','outback',color='blue',tow_package=True)
print(car)
{'manufacturer': 'subaru', 'model': 'outback', 'color': 'blue', 'tow_package': True}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长得丑就要多读书

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值