Python编程入门到实践Chapter-08

"""
Exercise 8-1:
消息

"""

def display_message():
    print("Python is my dish.")

display_message()
"""
Exercise 8-2:
喜欢的图书

"""

def favorite_book(title):
    print("One of my favorite book is <<%s>>." %title)

title = input("Which book is your favorite one? ")
favorite_book(title)
"""
Exercise 8-3:
T恤

"""

def make_shirt(size, word):
    print("The size of the T-shirt is %d." %size)
    print("The word will be printed is that %s." %word)

size = input("Input the size you want to make: ")
word = input("Input one word you want to print on that: ")

make_shirt(size, word)
"""
Exercise 8-4:
大号T恤

"""

def make_shirt(size='XL', word='I love python'):
    print("\nI am going to make a %s T-shirt." %size)
    print("It will say, %s." %word)

make_shirt()
make_shirt('L')
make_shirt('M', 'I love java')
"""
Exercise 8-5:
城市

"""

def describe_city(city, country='Japan'):
    print("%s is in %s." %(city, country))

describe_city('Tokyo')
describe_city('London')
describe_city('Osaka')
"""
Exercise 8-6:
城市名

"""

def city_country(city, country):
    print("\n%s, %s" %(city, country))

city_country(city= 'Tokyo', country='Japan')
city_country(city= 'Osaka', country='Japan')
city_country(city= 'Beijing', country='China')
"""
Exercise 8-7:
专辑

"""

def make_album(name_singer, name_album, number_track=0):
    album = {}
    album['artist'] = name_singer
    album['title'] = name_album
    if number_track:
        album['track'] = number_track
    return album

print(make_album('Dylan', 'My world'))
print(make_album('Jack', 'Your world'))
print(make_album('Mary', 'Our world', 8))
"""
Exercise 8-8:
用户的专辑

"""

def make_album(name_singer, name_album, number_track=0):
    album = {}
    album['artist'] = name_singer
    album['title'] = name_album
    if number_track:
        album['track'] = number_track
    return album

print("If you want to stop the circle, Input 'q' to stop anywhere!")

while True:
    name_singer = input("\nInput the name of singer: ")
    name_album = input("Input the name of album: ")
    number_track = input("Input th number of track: ")
    if name_album == 'q' or name_singer == 'q' or number_track == 'q':
        break

    ret = make_album(name_singer, name_album, int(number_track))
    print(ret)
"""
Exercise 8-9:
魔术师

"""

def show_magicians(name_magicians):
    for name in name_magicians:
        print(name)


name_magicians = ['Dylan', 'Jack', 'Mary']
show_magicians(name_magicians)
"""
Exercise 8-10:
了不起的魔术师

"""

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

def make_great(magicians):
    great_magicians = []
    while magicians:
        temp = magicians.pop()
        great_magicians.append('The great ' + temp)
    for great_magician in great_magicians:
        magicians.append(great_magician)

magicians = ['Dylan', 'Jack', 'Mary']
print(magicians)
make_great(magicians)
show_magicians(magicians)
"""
Exercise 8-11:
不变的魔术师

"""

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

def make_great(magicians):
    great_magicians = []
    while magicians:
        temp = magicians.pop()
        great_magicians.append('The great ' + temp)
    for great_magician in great_magicians:
        magicians.append(great_magician)

    return magicians

magicians = ['Dylan', 'Jack', 'Mary']
print(magicians)
print("\nChanged:")
show_magicians(make_great(magicians[:]))
print("\nBefore:")
show_magicians(magicians)
"""
Exercise 8-12:
三明治

"""

def make_sandwich(*items):
    print('')
    for item in items:
        print("I will add %s to your sandwich." %item)

make_sandwich('blackberry')
make_sandwich('roast beef', 'grilled cheese')
make_sandwich('roast beef', 'blackberry', 'grilled cheese')
"""
Exercise 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('Dylan', 'Smith',
                             location = 'Shenzhen',
                             major = 'CS',
                             gender = 'male')
print(user_profile)
"""
Exercise 8-14:
汽车

"""

def build_car(manufacturer, model, **car_info):
    car = {}
    car[manufacturer] = manufacturer
    car[model] = model
    for k, v in car_info.items():
        car[k] = v

    return car

car = build_car('mitsubishi', 'TX200', color = 'black', money = 60)
print(car)
"""
Exercise 8-15(1):
打印模型 (printing_functions.py)

"""

def print_models(unprinted_designs, completed_models):

    while unprinted_designs:
        current_design = unprinted_designs.pop()
        print("Printing model %s." %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)
"""
Exercise 8-15(2):
打印模型 (print_models.py)
Method one(import module_name)
"""

import python入门到实践.chapter_08.printing_functions as p

unprinted_designs = ['a', 'b', 'c']
completed_models = []

p.print_models(unprinted_designs, completed_models)
p.show_completed_models(completed_models)
"""
Exercise 8-15(2):
打印模型 (print_models.py)
Method two(from module_name import \
    function_name1, function_name2)
"""

from python入门到实践.chapter_08.printing_functions import \
    print_models, show_completed_models

unprinted_designs = ['a', 'b', 'c']
completed_models = []

print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
"""
Exercise 8-15(2):
打印模型 (print_models.py)
Method three(from module_name import \
    function_name1 as M, function_name2 as N)
"""

from python入门到实践.chapter_08.printing_functions import \
    print_models as p, show_completed_models as s

unprinted_designs = ['a', 'b', 'c']
completed_models = []

p(unprinted_designs, completed_models)
s(completed_models)
"""
Exercise 8-15(2):
打印模型 (print_models.py)
Method four(from module_name import *
"""
# Method four(from module_name import *)

from python入门到实践.chapter_08.printing_functions import *

unprinted_designs = ['a', 'b', 'c']
completed_models = []

print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)

猜你喜欢

转载自blog.csdn.net/weixin_41526797/article/details/85000479
今日推荐