赞
踩
#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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。