第8章 函数

8-1消息:

#8-1
def display_message():
    print('本章学习函数')
display_message()

8-2喜欢的图书:

#8-2
def favorite_book(title):
    print('One of my favorite books is '+title)
favorite_book('Alice in wonderland')

8-3T恤:

#8-3
def make_shirt(size,words):
    print('The shirt size is '+size+' '+words)
make_shirt('170','i love python')

8-4大号t恤:

#8-4
def make_shirt(size='big',words='i love python'):
    print('The shirt size is '+size+' '+words)
make_shirt( )
make_shirt('medium')
make_shirt('big','i love you')

8-5城市:

#8-5城市
def describe_city(city,country='china'):
    print(city+' is in '+country)
describe_city('nanjing')
describe_city('shenzhen')
describe_city('newyork','ameriaca')

8-6城市名:

#8-6
def city_country(city,country):
    message=city+','+country
    return message
message_1=city_country('shenzhen','china')
print(message_1.title())

8-7专辑:

#8-7:
def make_album(singer,alubm):
    message={'name':singer,'Music Album':alubm}
    return message
musician_1=make_album('草东没有派对','丑奴儿')
print(musician_1)
musician_2=make_album('吴青峰','起风了')
print(musician_2)
musician_3=make_album('雾虹','耗尽')
print(musician_3)
#要求二:
def make_album(singer,alubm,songs_num=' '):
    if songs_num:
        full=singer+' '+alubm+' '+songs_num
    else:
        full=singer+' '+alubm
    return full.title()
musician_1=make_album('草东没有派对','丑奴儿')
print(musician_1)
musician_2=make_album('吴青峰','起风了')
print(musician_2)
musician_3=make_album('雾虹','耗尽',songs_num='5首')
print(musician_3)

 8-8用户的专辑:

#8-8
def make_album(singer,alubm):
    message={'name':singer,'Music Album':alubm}
    return message
while True:
    print('\n请输入歌手名字和专辑名')
    print('\n如果想退出请输入字母q')
    name=input('歌手名字: ')
    if name =='q':
        break
    alubm=input('专辑名:')
    if alubm=='q':
        break

    full=make_album(name,alubm)
    print('\n歌手名是: '+name+'专辑名是:'+alubm)

8-9魔术师:

#8-9
def show_magicians(names):
    for name in names:
        print(name)
usernames=['魔术师一号','魔术师二号','魔术师三号']
show_magicians(usernames)

8-10了不起的魔术师:

#8-10
def make_great(magicians,completed):
    while magicians:
        current_designs=magicians.pop()
        print('The great '+current_designs)
        completed.append(current_designs)

def show_magicians(completed):
    print(completed)
magicians=['魔术师一号','魔术师二号','魔术师三号']
completed=[]
make_great(magicians)
show_magicians(completed)

8-11不变 的魔术师 :

#8-11
def make_great(magicians,completed):
    while magicians:
        current_designs=magicians.pop()
        print('The great '+current_designs)
        completed.append(current_designs)

def show_magicians(completed):
    print(completed)
magicians=['魔术师一号','魔术师二号','魔术师三号']
completed=[]
make_great(magicians[:],completed)
show_magicians(completed)
show_magicians(magicians)

 8-12三明治:

#8-12
def make_sandwich(*toppings):
    print('\n你点的三文治里面有以下配料:')
    for topping in toppings:
        print('-'+topping)
make_sandwich('番茄酱')
make_sandwich('沙拉酱','番茄酱')
make_sandwich('芝士酱','沙拉酱','番茄酱')

8-13用户简介:

#8-13
def bulid_profile(first,last,**user_info):
    profile={}
    profile['fitst_name']=first
    profile['last_name']=last
    for key,value in user_info.items():
        profile[key]=value
    return profile
user_profile =bulid_profile('chan','miu',location='shenzhen',field='finance')
print(user_profile)

8-14汽车:

#8-14
def car(manufacturers,size,**user_inf):
    '''总是接受制造商和型号, 还接受任意数量的关键字实参'''
    profile={}#profile 的空字典, 用于存储用户简介。
    profile['manu'] = manufacturers
    profile['si'] = size
    for key,value in user_inf.items():#我们遍历字典user_info 中的键—值对, 并将每个键—值对都加入到字典profile 中
        profile[key]=value
    return profile
user=car('baoma','yueye',
         nenghao='good',youhao='yiban',
         舒适度='好',宽敞度='大')
print (user)

  

posted @ 2020-05-29 21:24  猫山思  阅读(184)  评论(0)    收藏  举报