Python编程:从入门到实践 动手试一试 8.1-8.14

#8.1 messages

def display_message():
    print("In this chapter, you've learned how to use function.")
    
display_message()

#8.2 the books I like

def favorite_books(title):
    print("My favorite book is " + title.title() + "!")

favorite_books('call me by your name')

#8.3 T-shirt

def make_shirt(size_input, pattern_input):
    print("The T-shirt is about to be " + str(size_input) + " size and " + str(pattern_input) + ".")

make_shirt('L', 'SNAKE')
make_shirt(size_input="M", pattern_input="SNAKE")

#8.4 big size T-shirt

def make_shirt(size_input, pattern_input="I love Python"):
    print("The T-shirt is about to be " + str(size_input) + " size and " + str(pattern_input) + ".")

make_shirt('L')
make_shirt('M')
make_shirt('XXL', pattern_input="I love Computer")

#8.5 cities

def describe_cities(name = "Reykjavik", country = "Iceland"):
    print(name.title() + " is in " + country.title())

describe_cities("Shenzhen", "China")
describe_cities('New York', 'U.S.A')
describe_cities()

#8.6 cities’ name

def city_country(city, country):
    return print(city.title() + ", " + country.title())

city_country("shenzhen", "china")
city_country("shanghai", 'china')
city_country('london','britian')

#8.7 album

def make_album(name, name_album, number = ""):
    dic = {"Name: ": name.title(), "Name of Album: ": name_album.title(), "Numbers of Songs: ": number}
    return dic

information = make_album(name="benji", name_album="the voice of beauty")
print(information)

#8.8 users’ album

def make_album_2(name, name_album_2):
    dic_2 = {"Name:": name.title(), "Name of Album:": name_album_2.title()}
    return dic_2

print("Enter 'quit' to stop your search")
while True:
    n = input("Please enter singer's name: ")
    if n == "quit":
        break

    a = input("Please enter album's name: ")
    if a == "quit":
        break

    result = make_album_2(n, a)
    print(result)

#8.9 magician

names_of_magicians = ['benji', 'simone', 'karl', 'tommy', 'naomi']
def show_magicians():
    for print_name in names_of_magicians:
        print(print_name.title())

show_magicians()

#8.10 incredible magician

def show_magicians(magician_name_full):
    for magician_name in magician_name_full:
        print(magician_name.title())

def make_great(magician_names,magician_name_full):
    while magician_names:
        current_magician_name = magician_names.pop()
        magician_name_full.append("The Great " + current_magician_name)

magician_names = ['benji', 'simone', 'karl', 'tommy', 'naomi']
magician_name_full = []

make_great(magician_names[:],magician_name_full)
show_magicians(magician_name_full)

#8.11 unchanged magician

def show_magicians(magician_name_full):
    for magician_name in magician_name_full:
        print(magician_name.title())

def make_great(magician_names,magician_name_full):
    while magician_names:
        current_magician_name = magician_names.pop()
        magician_name_full.append("The Great " + current_magician_name)

magician_names = ['benji', 'simone', 'karl', 'tommy', 'naomi']
magician_name_full = []

make_great(magician_names[:],magician_name_full)
show_magicians(magician_name_full)

#8.12 sandwich

def sandwich(*toppings):
    print("Making a pizza with the following toppings: ")
    for topping in toppings:
        print("- " + topping)

sandwich('extra cheese', 'beef', 'chicken')

#8.13 uses’ information

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('Benji', 'He',
                                 location='Shenzhen',
                                 habbit='music', sexuality='male')
print(user_profile)

#8.14 cars

def make_cars(manufacturer, type, **other):
    car_profile = {}
    car_profile['manufacturer'] = manufacturer
    car_profile['type'] = type
    for key, value in other.items():
        car_profile[key] = value
    return car_profile

car = make_cars('subaru', 'outback', color='blue', tow_package=True)
print(car)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值