第八章课后作业

时间:2022-11-22 07:26:42

Chapter Eight

8-1 消息

def display_message():
    print("We learn functions in this chapter.")

display_message()

8-2 喜欢的图书

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

favorite_book('Alice in Wonderland')

8-3 T恤

def make_shirt(size, sentence):
    print('The T-shirt is in ' + size + ' size, with a sentence "' + sentence + '" on it.')


make_shirt('XL', 'Hello World!')

8-4 大号T恤

def make_shirt(size='L', sentence='I love Python'):
    print('The T-shirt is in ' + size + ' size, with a sentence "' + sentence + '" on it.')


make_shirt()
make_shirt('M')
make_shirt('S', 'Python is fun')

8-5 城市

def describe_city(name, country='China'):
    print(name + ' is in ' + country + '.')


describe_city('Shanghai')
describe_city('New York', 'USA')
describe_city('Lendon', 'UK')

8-6 城市名

def city_country(city, country):
    return city + ', ' + country


print(city_country('New York', 'USA'))
print(city_country('Shanghai', 'China'))
print(city_country('Berlin', 'Germany'))

8-7 专辑

def make_album(singer, album, song=0):
    album = {
        'singer': singer,
        'album': album
    }
    if song:
        album['song'] = song
    return album


print(make_album('Two Steps From Hell', 'Battlecry', 12))
print(make_album('Matt B', 'LOVE AND WAR'))
print(make_album('OMFG', 'Hello', 4))

8-8 用户的专辑

def make_album(singer, album, song=0):
    made_album = {
        'singer': singer,
        'album': album
    }
    if song:
        made_album['song'] = song
    return made_album


while True:
    signer = input('Please enter the signer name(enter "quit" to quit):\t')
    if signer == 'quit':
        break
    album = input('Please enter the album name:\t')
    print(make_album(signer, album))

8-9 魔术师

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


magicians = ['Zhang San', 'Li Si', 'Wang Ermazi']
show_magicans(magicians)

8-10 了不起的魔术师

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


def make_great(magicians):
    for i in range(0, len(magicians)):
        magicians[i] = 'the Great ' + magicians[i]


magician_list = ['Zhang San', 'Li Si', 'Wang Ermazi']
show_magicans(magician_list)
make_great(magician_list)
show_magicans(magician_list)

8-11 不变的魔术师

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


def make_great(magicians):
    for i in range(0, len(magicians)):
        magicians[i] = 'the Great ' + magicians[i]
    return magicians


magician_list = ['Zhang San', 'Li Si', 'Wang Ermazi']
show_magicans(magician_list)
show_magicans(make_great(magician_list[:]))
show_magicans(magician_list)

8-12 三明治

def make_sandwich(*mixtures):
    print('The sandwich has following mixtures:')
    for mixture in mixtures:
        print('\t' + mixture)


make_sandwich('beef', 'meat')
make_sandwich('meat', 'vegetable', 'fruit')
make_sandwich('potato', 'beef', 'tomato')

8-13 用户简介

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('James', 'Brown', age=18, location='Guangzhou', field='CS')
print(user_profile)

8-14 汽车

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


car = make_car('sabaru', 'outback', color='blue', two_package=True)
print(car)

8-15 打印模型

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)
import print_function


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

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

8-16 导入

# # Ver 1
# import city_plus
# city_plus.describe_city('Shanghai')
# city_plus.describe_city('New York', 'USA')
# city_plus.describe_city('Lendon', 'UK')

# # Ver 2
# from city_plus import describe_city
# describe_city('Shanghai')
# describe_city('New York', 'USA')
# describe_city('Lendon', 'UK')

# # Ver 3
# from city_plus import describe_city as des
# des('Shanghai')
# des('New York', 'USA')
# des('Lendon', 'UK')

# # Ver 4
# import city_plus as cp
# cp.describe_city('Shanghai')
# cp.describe_city('New York', 'USA')
# cp.describe_city('Lendon', 'UK')

# Ver 5
from city_plus import *
describe_city('Shanghai')
describe_city('New York', 'USA')
describe_city('Lendon', 'UK')