第八章

8-1:

def display_message():
print('我们正在学函数')

display_message()


8-2:

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

favorite_book('Alice in Wonderland')


8-3:

def make_shirt(size, char):
print('size is', size, ', character is', char)

make_shirt(4, 'T')
make_shirt(size=8, char='Y')


8-4:

def make_shirt(size = 'large', char = 'I love Python'):
print('size is', size, ', character is', char)

make_shirt()
make_shirt(size = 'medium')
make_shirt(size = 'small', char='hello world')


8-5:

def describe_city(name='Reykjavik', country='Iceland'):
print(name, 'is in', country)

describe_city()
describe_city('Guangzhou', 'China')
describe_city('Beijing', 'China')


8-6:

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

print(city_country('Santiago', 'Chile'))
print(city_country('Beijing', 'China'))
print(city_country('Guangzhou', 'China'))

8-7:

def make_album(name, album):
return {'singer_name': name, 'singer_album': album}

print(make_album('刘若英', '后来'))
print(make_album('周杰伦', '范特西'))
print(make_album('刘欢', '好汉歌'))


8-8:

def make_album(name, album):
return {'singer_name': name, 'singer_album': album}

while True:
msg1 = input("Please input a singer's name(q to quit): ")
if msg1 == 'q':
break
msg2 = input("Please input a singer's album: ")
print(make_album(msg1, msg2))


8-9:

def show_magicians(magi):
for man in magi:
print(man, end=' ')

magicians = ['Jay', 'Alan', 'John', 'Mary']
show_magicians(magicians)


8-10:

def show_magicians(magi):
for man in magi:
print(man, end=' ')
def make_great(magi):
for i in range(len(magi)):
magi[i] = 'The Great' + magi[i]

magicians = ['Jay', 'Alan', 'John', 'Mary']
show_magicians(magicians)
make_great(magicians)
show_magicians(magicians)


8-11:

def show_magicians(magi):
for man in magi:
print(man, end=' ')
def make_great(magi):
tmp = []
for i in range(len(magi)):
tmp.append('The Great ' + magi[i])
return tmp

magicians = ['Jay', 'Alan', 'John', 'Mary']
show_magicians(magicians)
show_magicians(make_great(magicians[:]))


8-12:

def add_sandwich(*adds):
print('Making a sandwich with the following toppings:')
for t in adds:
print(t)

add_sandwich('aaa')
add_sandwich('aaa', 'bbb')
add_sandwich('aaa', 'bbb', 'ccc')


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

my_profile = build_profile('Jair', 'Zhu', age='20', hobby='game', specialty='sleep')
print(my_profile)


8-14:

def make_car(producer, model, **info):
tmp = {'Producer': producer, 'Model': model}
for key, value in info.items():
tmp[key] = value
return tmp

car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值