《python编程从入门到实践》第8章 函数(练习)

P116作业

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

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

P121作业

#8-3
def make_shirt(size,type):
    print('这件衬衫尺码是'+size.title()+',图案是'+type.title()+'。')

#位置实参
make_shirt('s','可乐')
#关键字实参
make_shirt(size = 's',type = '可乐')

#8-4
def make_shirt(size,type = 'I love Python'):
    print('这件衬衫尺码是'+size.title()+',图案是'+type.title()+'。')
    
make_shirt('l')
make_shirt('m')
make_shirt('s',type = 'ok')


#8-5
def describe_city(city,country = 'China'):
    print(city.title()+' is in '+country+'.')
    
describe_city('changchun')
describe_city('tokyo',country = 'Japan')

P125作业

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

word = city_country('santiago','chile')
print(word)

#8-7
def make_album(singer,album,number=''):
    if number:
        person = {'singer':singer,'album':album,'number':number}
        return person
    person = {'singer':singer,'album':album}
    return person

    
a = make_album('周杰伦','叶惠美')
b = make_album('许嵩','清明雨上')
print(a)
print(b)
c = make_album('许嵩','清明雨上',3)
print(c)


#8-8
def make_album(singer,album):
    person = {'singer':singer,'album':album}
    return person
    
while True:
    print("(enter 'q' at any time to quit)")
    
    a = input('Please input a singer:')
    if a == 'q':
        break
        
    b = input("Please input the singer's album:")
    if b == 'q':
        break
        
    c = make_album(a,b)
    print(c)

P129作业

#8-9
def show_magicians(magicians):
    for magician in magicians:
        print(magician)
        
magicians = ['a','b','c','d']
show_magicians(magicians)


#8-10

def show_magicians(magicians):
    while magicians:
        make_great(magicians)
        break

def make_great(magicians):
    for magician in magicians:
        print('the great '+magician)
        
        
magicians = ['a','b','c','d']
show_magicians(magicians)


#8-11
def show_magicians(magicians):
    while magicians:
        make_great(magicians)
        break

def make_great(magicians):
    for magician in magicians:
        print('the great '+magician)
        
        
magicians = ['a','b','c','d']
show_magicians(magicians)

print(magicians)

P132作业

#8-12
def sandwich(*foods):
    print('顾客点了这些三明治:')
    for food in foods:
        print(food)

sandwich('a')
sandwich('a','b','c')
sandwich('a','dfadf','adfe','adfeefd')


#8-13
print('')
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('晨','谢',年龄 = '23', 性别 = '女', 家乡 = '长春')
print(user_profile)


#8-14
print('')
def make_car(manufacturer,model,**car_info):
    profile = {}
    profile['manufacturer'] = manufacturer
    profile['model'] = model
    for key,value in car_info.items():
        profile[key] = value
    return profile
    
car_profile = make_car('sabaru','outback',color = 'blue',tow_package = True)
print(car_profile)

P137作业

模块 printing_functions.py

def print_models(unprinted_designs, completed_models):
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        print("Printing model: " + current_design)
        completed_models.append(current_design)
        
        
def show_completed_models(completed_models):
    print("\nThe following models have been printed:")
    for completed_model in completed_models:
        print(completed_model)

模块 magician.py

def show_magicians(magicians):
    for magician in magicians:
        print(magician)

P137作业+调用模块的五种方式:

#8-15

import printing_functions

unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []

printing_functions.print_models(unprinted_designs,completed_models)
printing_functions.show_completed_models(completed_models)

#8-16
print('')
import magician
magicians = ['ab','cd','ef','gh']
magician.show_magicians(magicians)

print('')
from magician import show_magicians
magicians = ['ab','cd','ef','gh']
show_magicians(magicians)

print('')
from magician import show_magicians as mg
magicians = ['ab','cd','ef','gh']
mg(magicians)

print('')
import magician as m
magicians = ['ab','cd','ef','gh']
m.show_magicians(magicians)

print('')
from magician import *
magicians = ['ab','cd','ef','gh']
show_magicians(magicians)

 

不足之处望多加指正。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值