python从入门到实践 第八章习题(高级编程技术 week4-2)

python从入门到实践 第八章习题(高级编程技术 week4-2)

这一节课主要讲的是在python中用户输入的处理以及while循环的编写。

8.1 定义函数

8-1 消息

def display_message():
    print('本章我学的是函数')
    print('I learn the function in python.')

display_message()

8-2 喜欢的图书

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

favorite_book('Alice in Wonderland')

8.2 传递实参

主要要知道两点。
1. 位置实参是根据实参的位置,将实参传递给对应的形参
1. 关键字形参是根据关键字,将实参传递个对应的形参

当然,也可以根据需要,给函数的参数提前设定好默认值。具体的操作方式是在函数定义的时候给形参先赋值,那么在调用的时候就具有默认值了。

8-3 T恤

def make_shirt(size, message):
    print('The T-shirt is ',size,' size.')
    print('message=',message,' is on the T-shirt.')

make_shirt('XXL', 'It's my T-shirt')

8-5 城市

def describe_city(city_name, city_country='Chine'):
    print(city_name.title(), ' is in ', city_contry, '.')

describe_city('guangzhou')
describe_city('Beijing')
describe_city('New York', 'USA')

8.3 返回值

返回值通过在函数结尾放置return XXXX语句,将某一个操作数传递给调用者。
这里有一个设置可选参数的小技巧,书中的例子是将可选参数放到形参列表末尾,并为该可选参数提供空的默认值。那么,在程序中,就可以根据这个形参是否为空来决定程序的功能,也就是实现了可选参数。

8-6 城市名

def city_country(city_name, city_country):
    return (city_name.title()+','+city_country.title())

ciry_country('guangzhou')
ciry_country('Beijing')
ciry_country('New York', 'USA')

8.7/8-8 用户的专辑

def make_album(singer_name, album_name, song_number = ''):
    album = {}
    album['singer_name'] = singer_name
    album['album_name'] = album_name
    if song_number:
        album['song_number'] = song_number
    return album

test1 = make_album('wo', 'think')
print(test1)
test2 = make_album('ta', 'sing', 3)
print(test2)

while 1:
    singer_name = input('Please enter the name of singer : ')
    album_name = input('Please enter the name of album : ')
    album_dict = make_album(singer_name, album_name)
    print(album_dict)
    quit_message = input('Do you want to continue to create the album? Y/N')
    if quit_message.upper() == 'N':
        break


print('Exit the program!')

8.4 传递列表

注意这里向函数传递列表,传递的是指针,对列表的修改会直接反映到调用者中的列表。
如果要放置函数修改列表,就创建一个新的列表传进去,方法和以前学过的一样,用切片的方式创建。

8-9/10/11 魔术师

def show_magicians(names):
    for name in names:
        print('The name of the magician is ', name.title(), '.')

def make_great(names):
    i = 0
    while i < len(names):
        names[i] = 'the Great '+names[i]
        i = i + 1
    return names


magics = ['Jack', 'Mike', 'Lily']
show_magicians(magics)

# make_great(magics)
# show_magicians(magics)

great_magics = make_great(magics[:])
show_magicians(magics)
show_magicians(great_magics)

8.5 传递任意数量的实参

可以通过向函数显式的传递一个类似指针的东西,这个指针就能够访问到传递到该函数的任意个参数。

实际上,*号创建的并不是指针,而是空元组,他能将收到的所有参数都放到这个元组中。因此在函数体中,能够对这个元组直接进行迭代。

然后,还能通过**给函数传递任意数量的关键字实参。这是通过将关键字实参组织成一个字典传进函数从而得以实现的。

8-12

def create_sandwich(*inds):
    print('This sandwich contains :')
    for ind in inds:
        print(ind)

create_sandwich('bread','potato','tomato')

8-14

def make_car(car_brand, car_model, **car_info):
    print('The brand of car is ', car_brand, '.')
    print('The model of car is ', car_model, '.')
    car_info['car_brand'] = car_brand
    car_info['car_model'] = car_model
    return car_info

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

8.6 将函数存储在模块中

这里使用了一些导入的语句进行测试

猜你喜欢

转载自blog.csdn.net/wyfwyf12321/article/details/79780777