【HW】 第八章作业 2018.3.29

时间:2022-11-22 09:09:37

Python代码:

# Chapter 8 homework by szh 2018.3.28
print("\n8.1")
def display_message():
    print("This chapter we study the function.")
display_message()


print("\n8.2")
def favorite_book(title):
    print(" One of my favorite books is " + title)
favorite_book('Alice in Wonderland')


print("\n8.3")
def make_shirt(size, words):
    print("The Tshirt's size is " + size + " and the words is " + words)
make_shirt('middle', 'Hello world!')


print("\n8.4")
def make_shirt2(size='big', words='I love Python!'):
    print("The Tshirt's size is " + size + " and the words is " + words)
make_shirt2()
make_shirt2('middle')
make_shirt2('small', 'I love C++!')


print("\n8.5")
def describe_city(city, country = 'Iceland'):
    print(city + ' is in ' + country)
describe_city('Reykjavik')
describe_city('London', 'England')
describe_city('Beijing', 'China')


print("\n8.6")
def city_country(city, country):
    return city + ", " + country
print(city_country('Beijing', 'China'))
print(city_country('London', 'England'))
print(city_country('Shanghai', 'China'))


print("\n8.7")
def make_album(name, album_name):
    return {'name':name, 'album name':album_name}
print(make_album('Alice', 'I Love You ~'))


def make_album2(name, album_name, number = None):
    if number:
        return {'name':name, 'album name':album_name, 'number':number}
    else:
        return {'name':name, 'album name':album_name}
print(make_album2('Alice', 'I Love You ~'))
print(make_album2('Bob', 'Break out', 12))


print("\n8.9")
magicians = ['Alice', 'Bob', 'Eric', 'Jack']
def show_magicians(magicians):
    for magician in magicians:
        print(magician)
show_magicians(magicians)


print("\n8.10")
def make_great(magicians):
    for i in range(len(magicians)):
        magicians[i] = "The Great " + magicians[i] 
    return magicians
magicians = make_great(magicians)
show_magicians(magicians)


print("\n8.11")
magicians = ['Alice', 'Bob', 'Eric', 'Jack']
Great_magicians = make_great(magicians[:])
print("The Great magicians: ")
show_magicians(Great_magicians)
print("\nThe orginal magicians: ")
show_magicians(magicians)


输出结果:

8.1
This chapter we study the function.

8.2
 One of my favorite books is Alice in Wonderland

8.3
The Tshirt's size is middle and the words is Hello world!

8.4
The Tshirt's size is big and the words is I love Python!
The Tshirt's size is middle and the words is I love Python!
The Tshirt's size is small and the words is I love C++!

8.5
Reykjavik is in Iceland
London is in England
Beijing is in China

8.6
Beijing, China
London, England
Shanghai, China

8.7
{'name': 'Alice', 'album name': 'I Love You ~'}
{'name': 'Alice', 'album name': 'I Love You ~'}
{'name': 'Bob', 'album name': 'Break out', 'number': 12}

8.9
Alice
Bob
Eric
Jack

8.10
The Great Alice
The Great Bob
The Great Eric
The Great Jack

8.11
The Great magicians:
The Great Alice
The Great Bob
The Great Eric
The Great Jack

The orginal magicians:
Alice
Bob
Eric
Jack