代码:
#8.1
def display_message():
print(8.1)
print("display_message function runs.")
print()
display_message()
#8.2
def favorite_book(book_name):
print("My favorite book is "+book_name+".")
print(8.2)
book_name = "Lolita"
favorite_book(book_name)
print()
#8.5
def describe_city(city,country = "China"):
print(city+" is in "+country+".")
print(8.5)
describe_city("Guangzhou")
describe_city("Beijing")
describe_city("Tokyo", "Japan")
print()
#8.6
def city_country(city,country):
return city+", "+country
print(8.6)
cities = {"Guangzhou":"China","Beijing":"China","Tokyo":"Japan"}
for city,country in cities.items():
print(city_country(city,country))
print()
#8.9
def show_magicians(magicians):
for magician in magicians:
print(magician)
print(8.9)
magicians = ['Tom','Jack','Zet']
show_magicians(magicians)
print()
#8.10
def make_great(magicians):
great_magicians = []
while magicians:
great_magicians.append("the Great " + magicians.pop())
while great_magicians:
magicians.append(great_magicians.pop())
return magicians
print('8.10')
magicians_copy = magicians.copy()
make_great(magicians)
show_magicians(magicians)
print()
#8.11
print(8.11)
great_magicians = make_great(magicians_copy[:])
show_magicians(magicians_copy)
show_magicians(great_magicians)
print()
输出:
8.1
display_message function runs.
8.2
My favorite book is Lolita.
8.5
Guangzhou is in China.
Beijing is in China.
Tokyo is in Japan.
8.6
Guangzhou, China
Beijing, China
Tokyo, Japan
8.9
Tom
Jack
Zet
8.10
the Great Tom
the Great Jack
the Great Zet
8.11
Tom
Jack
Zet
the Great Tom
the Great Jack
the Great Zet