高级编程技术 - 第八章练习

时间:2021-09-10 20:29:58

8-1 消息

# message.py
def display_message():
    print("We are learning functions this chapter.")
display_message()
Output

We are learning functions this chapter.

8-2 喜欢的图书

# favorite.py
def favorite_book(title):
    print("One of my favorite books is Alice in " + title + ".")
favorite_book("Wonderland")
Output

One of my favorite books is Alice in Wonderland.

8-3 T恤

# T-shirt.py
def make_shirt(size, painting):
    print("T-shirt size:" + size + " painting:" + painting)
make_shirt("M","Hello")
Output

T-shirt size:M painting:Hello

8-4 大号T恤

# big_T-shirt.py
def make_shirt(size, painting = 'I love Python'):
    print("T-shirt size:" + size + " painting:" + painting)
make_shirt('L')
make_shirt('M')
make_shirt("M","Hello")
Output

T-shirt size:L painting:I love Python
T-shirt size:M painting:I love Python
T-shirt size:M painting:Hello

8-5 城市

# city.py
def describe_city(city,country = 'China'):

    print(city + " is in " + country)
describe_city('Shanghai')
describe_city('Silicon Valley','the USA')
describe_city('Guangzhou','China')
Output

Shanghai is in China
Silicon Valley is in the USA
Guangzhou is in China

8-6 城市名

# city_name.py
def city_country(city,country = 'China'):
    return city + ", " + country
print(city_country('Shanghai'))
print(city_country('Silicon Valley','the USA'))
Output

Shanghai, China
Silicon Valley, the USA

8-9 魔术师

# magician.py
def show_magicians(magicias):
    for magician in magicians:
        print(magician)
magicians = ['A','B','C']
show_magicians(magicians)
Output

A
B
C

8-10 了不起的魔术师

# great_magician.py
def make_great(magicians):
    for i in range(0,len(magicians)):
        magicians[i] = "the Great "+ magicians[i]
def show_magicians(magicians):
    for magician in magicians:
        print(magician)
magicians = ['A','B','C']
make_great(magicians)
show_magicians(magicians)
Output

the Great A
the Great B
the Great C