函数

第八章

函数
8-2

def display_message(chapter):
    """简单的语句"""
    print("本章学习 " + chapter)

display_message('函数')
本章学习 函数
def favorite_book(title):
    """tushu"""
    print("woxihuaned " + title + ".")
  
favorite_book('python')
woxihuaned python.

8-3

def make_shirt(chima, ziyang):
    """T恤"""
    print("T恤de chima shi :" + chima)
    print("T恤" + chima + "de ziyangshi " + ziyang)

make_shirt('xl','python')
make_shirt(chima='xl',ziyang='pthon') 

8-4

def make_shirt(ziyang='LOVE',chima='XXL' ):
    """T恤"""
    print("T恤de chima shi :" + chima)
    print("T恤" + chima + "de ziyangshi " + ziyang)

make_shirt()
make_shirt(chima='xl')
make_shirt(ziyang='abc') 

8-5

def describe_city(name,ciaty_country='zhongguo'):
    """chengshi"""
    print(name + " in " + ciaty_country)

describe_city('jiaxing')
describe_city('sichuan')
describe_city('nowyork',ciaty_country='meiguo') 

8-6

def city_cpuntry(name, country):
    """"""
    full = name + ',' + country
    return full.title()
message = city_cpuntry('jiaxing', 'zhongguo')
print(message)

8-7

def make_album(name,album,number=''):
    """fanhuizidain"""
    albums = {'name_a': name,'album_a': album}
    if number:
        albums['number'] = number
    return albums
mussage = make_album('dengziqi','xihuanni',number=2)
print(mussage)
{'name_a': 'dengziqi', 'album_a': 'xihuanni', 'number': 2}

8-8

def make_album(name,album):
    """fanhuizidain"""
    albums = {'name_a': name,'album_a': album}
    return albums
while True:
    print("\nplease tell me your love song: ")
    print("(enter 'q' at any time to quit)")
    f_name = input("name: ")
    if f_name == 'q':
        break
    l_labum = input("album: ")
    if l_labum == 'q':
        break
mussage = make_album(f_name,l_labum)
print(mussage)
please tell me your love song: 
(enter 'q' at any time to quit)
name: dengziqi
albumxihuani

please tell me your love song: 
(enter 'q' at any time to quit)
name: q
{'name_a': 'q', 'album_a': 'xihuani'}

8-9

def show_magicians(names):
    """moshushi"""
    for name in names:
        print(name)
        
magicians = ['刘谦','大卫.科波菲尔', '大卫.布莱恩','约克']
show_magicians(magicians)
刘谦
大卫.科波菲尔
大卫.布莱恩
约克

8-10

def show_great_magicians(names):
    """moshushi"""
    for name in names:
        msg = "the Great magician: " + name
        print(msg)
        
magicians = ['刘谦','大卫.科波菲尔', '大卫.布莱恩','约克']
show_great_magicians(magicians)
the Great magician: 刘谦
the Great magician: 大卫.科波菲尔
the Great magician: 大卫.布莱恩
the Great magician: 约克

8-11

def show_magicians(magicians):
    for magician in magicians:
        print(magician)
def make_great(magician):
    for name in magician:
        print("\nthe Great " + name)
        
magicians = ['刘谦','大卫.科波菲尔', '大卫.布莱恩','约克']
magician = []

show_magicians(magicians)
make_great(magicians)

        
刘谦
大卫.科波菲尔
大卫.布莱恩
约克

the Great 刘谦

the Great 大卫.科波菲尔

the Great 大卫.布莱恩

the Great 约克

8-12

def make_sanwhich(*toppings):
    print("\nMaking asanwhich with following toppings: ")
    for topping in toppings:
        print("-" + topping)

make_sanwhich('popp')
make_sanwhich('faegufi', 'awfb','wfaggr')
Making asanwhich with following toppings: 
-popp

Making asanwhich with following toppings: 
-faegufi
-awfb
-wfaggr

8-13

def build_profile(first, last, **use_info):
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in use_info.items():
        profile[key] = value
    return profile

user_profile = build_profile('he', 'yuan wen',
                            age=23,
                            hight=175)
print(user_profile)
{'first_name': 'he', 'last_name': 'yuan wen', 'age': 23, 'hight': 175}

8-14

def build_profile(first, last, **use_info):
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in use_info.items():
        profile[key] = value
    return profile

user_profile = build_profile('subaru', 'outback',
                            color='green',
                            tow_package=True)
print(user_profile)
{'first_name': 'subaru', 'last_name': 'outback', 'color': 'green', 'tow_package': True}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值