python编程:从入门到实践-第八章练习

#-*-coding:utf-8 -*-
# 8-1
def display_message ():
print ( 'function!' )

# 8-2
def favorite_book ( title ):
print ( 'One of my favorite book is' ,title)

# 8-3
def make_shirt ( size , words ):
print ( 'T-shirt:' ,words,size)

make_shirt( 8 , 'i can fly' )
make_shirt( size = 9 , words = '404 not found' )

# 8-4
def make_shirt ( size = 'big' , words = 'i love you' ):
print ( 'T-shirt:' ,words,size)

make_shirt()
make_shirt( 'middle' )
make_shirt( words = 'dfsdf' )

# 8-5
def describe_city ( name , country = 'china' ):
print (name, 'is in' ,country)

describe_city( 'shanghai' )
describe_city( 'new York' , 'USA' )
describe_city( 'beijing' )

# 8-6
def city_country ( city , country ):
return '"' + city + ',' + country + '"'

print (city_country( 'beijing' , 'china' ))
print (city_country( 'Santiago' , 'Chile' ))
print (city_country( 'new_york' , 'USA' ))


# 8-7
def make_album ( name , album ):
return { 'name' :name, 'album' :album}

print (make_album( 'xxx' , 'yyy' ))
print (make_album( 'zhou' , 'dao' ))
print (make_album( 'lady_' , 'crazy' ))

# 8-8

while True :
n = input ( 'name:' )
a = input ( 'album:' )
if n == 'q' or a == 'q' :
break
else :
print (make_album(n,a))

# 8-9
def show_magicians ( name ):
for i in name:
print (i)

# 8-10
def make_great ( n ):
l = []
for i in n:
i = 'the great ' + i
l.append(i)
n = l[:]

a = [ 'xyz' , 'abc' , 'lmn' ]
make_great(a)
show_magicians(a)

# 8-11
a = [ 'xyz' , 'abc' , 'lmn' ]
def make_great ( n , m ):
for i in n:
i = 'the great ' + i
m.append(i)

b = []
make_great(a,b)
show_magicians(a)
show_magicians(b)

# 8-12
def pizza ( * i ):
for n in i:
print (n)

pizza( 'aaa' , 'bbb' , 'ccc' )
pizza( '333' , '444' , '555' )
pizza( '123' , '234' , '345' )

# 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

p = build_profile( 'xiao' , 'jianzhe' , hobbey = 'football' , skills = 'none' , weight = 'secret' )
print (p)

# 8-14

def make_car ( manufacturer , model , ** m ):
car_l = {}
car_l[ 'manufacturer' ] = manufacturer
car_l[ 'model' ] = model
for k,v in m:
car_l[k] = m
return car_l

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

# 8-15
# 加上 from printing_model import * 即可





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值