Python编程 从入门到实践 第八章习题

8-2 喜欢的图书:
def favorite_book(title):
    """"喜欢的图书"""
    print("One of my favorite book is " + title.title() + ".")

favorite_book("Harry Potter")

输出:

One of my favorite book is Harry Potter.


8-4 大号T恤:

def make_shirt(size, word = "I love Python"):
    print("\nSize: " + size )
    print("Words: " + word)

make_shirt("L")
make_shirt("M")
make_shirt("S",word = "I love C++")

输出:

Size: L
Words: I love Python

Size: M
Words: I love Python

Size: S
Words: I love C++


8-7 专辑:

def make_album(singer_name, ablum_name, numebr = ""):
    """返回专辑的信息"""
    ablum = {'singer': singer_name, 'ablum': ablum_name}
    if numebr:
        ablum['number'] = numebr;
    return ablum

ablum = make_album("Taylor Swift","Reputation",15)
print(ablum)

ablum2 = make_album("Taylor Swift", "Fearless")
print(ablum2)
输出:
{'singer': 'Taylor Swift', 'ablum': 'Reputation', 'number': 15}
{'singer': 'Taylor Swift', 'ablum': 'Fearless'}


8-8 用户的专辑

def make_album(singer_name, ablum_name, numebr = ""):
    """返回专辑的信息"""
    ablum = {'singer': singer_name, 'ablum': ablum_name}
    if numebr:
        ablum['number'] = numebr;
    return ablum

while True:
    print("\nPlease enter the name of singer: ")
    singer_name = input("Singer Name:")

    if singer_name == 'q':
        break;

    print("Please enter the name of ablum: ")
    ablum_name = input("Ablum Name: ")

    if ablum_name == 'q':
        break;

    ablum = make_album(singer_name,ablum_name)
    print(ablum)
输出:
Please enter the name of singer: 
Singer Name:Taylor Swift
Please enter the name of ablum: 
Ablum Name: Reputation
{'singer': 'Taylor Swift', 'ablum': 'Reputation'}

Please enter the name of singer: 
Singer Name:q


8-9 魔术师:

def show_magicians(names):
    for name in names:
        msg = "Hello, " + name.title() + "!"
        print(msg)

names = ['Alex', 'Leo', 'Mike']
show_magicians(names)
输出:
Hello, Alex!
Hello, Leo!
Hello, Mike!

8-10 了不起的魔术师:

def show_magicians(names):
    for name in names:
        msg = "Hello, " + name.title() + "!"
        print(msg)

def make_great(names,new_names):
    while names:
        current_name = names.pop()
        current_name = "the Great " + current_name
        new_names.append(current_name)

names = ['Alex', 'Leo', 'Mike']
new_names = []

print("The Original magician: ")
show_magicians(names)

make_great(names,new_names)
print("\nThe present magicians: ")
show_magicians(new_names)

输出:

The Original magician: 
Hello, Alex!
Hello, Leo!
Hello, Mike!

The present magicians: 
Hello, The Great Mike!
Hello, The Great Leo!
Hello, The Great Alex!

8-14 汽车:

def make_car(factory, type, **car_info):
    car = {}
    car['factory'] = factory
    car['type'] = type
    for key, value in car_info.items():
        car[key] = value
    return car

car_info = make_car('subaru', 'outback', color = 'blue', tow_packet = True)
print(car_info)

输出:

{'factory': 'subaru', 'type': 'outback', 'color': 'blue', 'tow_packet': True}

8-15 打印模型

#print_funtions.py

def print_fun(name):
    print("Hello, " + name)
#print_models.py
import print_funtions as p_fun

p_fun.print_fun("Alex")
输出:
Hello, Alex









猜你喜欢

转载自blog.csdn.net/sysu_alex/article/details/79772531