8-1 消息
def display_message():
print("We have learned funtion in this chapter!")
display_message()
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
We have learned funtion in this chapter!
Process finished with exit code 0
8-2 喜欢的图书
def favorite_book(Title):
print('One of my favorite books is ',Title.title())
favorite_book('Pride and Prejudice')
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
One of my favorite books is Pride And Prejudice
Process finished with exit code 0
8-3 T恤
def make_shirt(size,character):
print("The T-shirt's size is",size,",and print ",character,'on it!')
make_shirt('medium','Hello World')
make_shirt(character='Paul',size='large')
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
The T-shirt's size is medium ,and print Hello World on it!
The T-shirt's size is large ,and print Paul on it!
Process finished with exit code 0
8-4 大号T恤
def make_shirt(size='large',character='I love Python'):
print("The T-shirt's size is",size,",and print ",character,'on it!')
make_shirt()
make_shirt('medium')
make_shirt(character='Hello World')
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
The T-shirt's size is large ,and print I love Python on it!
The T-shirt's size is medium ,and print I love Python on it!
The T-shirt's size is large ,and print Hello World on it!
Process finished with exit code 0
8-5 城市
def describe_city(city,country='China'):
print(city,"is in",country)
describe_city('ShangHai')
describe_city('GuangZhou')
describe_city('Reykjavik','Iceland')
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
ShangHai is in China
GuangZhou is in China
Reykjavik is in Iceland
Process finished with exit code 0
8-6 城市名
def city_country(city,country='China'):
return city+","+country
print(city_country('Shanghai'))
print(city_country('Guangzhou'))
print(city_country('Reykjavik','Iceland'))
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
Shanghai,China
Guangzhou,China
Reykjavik,Iceland
Process finished with exit code 0
8-7 专辑
def make_album(singer,name,nums=0):
album={}
album['Singer']=singer
album['Name']=name
if nums!=0:
album['Numbers:']=nums
return album
print(make_album('Taylor Swift','reputation',15));
print(make_album('Taylor Swift','On and On'))
print(make_album('Taylot Swift','1989'))
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
{'Singer': 'Taylor Swift', 'Name': 'reputation', 'Numbers:': 15}
{'Singer': 'Taylor Swift', 'Name': 'On and On'}
{'Singer': 'Taylot Swift', 'Name': '1989'}
Process finished with exit code 0
8-9 魔术师
def show_magicians(magicians):
print("magicians' name:")
for magician in magicians:
print(magician,end=' ')
print()
show_magicians(['Liu','Li','Wang','Zhao'])
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
magicians' name:
Liu Li Wang Zhao
Process finished with exit code 0
8-10 了不起的魔术师
def show_magicians(magicians):
print("magicians' name:")
for magician in magicians:
print(magician,end='、')
print()
def make_magicians(magicians):
for magician in magicians:
magicians.append(magician+' the Great')
magicians.remove(magician)
magicians=['Li','Chan','Chen','Wang']
show_magicians(magicians)
make_magicians(magicians)
show_magicians(magicians)
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
magicians' name:
Li、Chan、Chen、Wang、
magicians' name:
Chan、Wang、Chen the Great、Li the Great the Great the Great、
Process finished with exit code 0
8-12 三明治
def sandwich(*batching):
print("The sandwich has",end='')
for x in batching:
print(x,end=',')
print()
sandwich('mushrooms','extra cheese')
sandwich('green peppers','tomato','potato')
sandwich('mushrooms')
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
The sandwich hasmushrooms,extra cheese,
The sandwich hasgreen peppers,tomato,potato,
The sandwich hasmushrooms,
Process finished with exit code 0
8-14 汽车
def make_car(manufacturer,model,**info):
car={}
car['Manufacturer']=manufacturer
car['Model']=model
for key,item in info.items():
car[key]=item
return car
print(make_car('subaru','outback',color='blue',tow_package=True))
print(make_car('BMW','520i',color='red',seat='Leather'))
print(make_car('Volvo','S80',color='black',seat='fabric'))
Output:
/Library/Frameworks/Python.framework/Versions/3.6/bin/learning/bin/python /Users/macbook/PycharmProjects/learning/test
{'Manufacturer': 'subaru', 'Model': 'outback', 'color': 'blue', 'tow_package': True}
{'Manufacturer': 'BMW', 'Model': '520i', 'color': 'red', 'seat': 'Leather'}
{'Manufacturer': 'Volvo', 'Model': 'S80', 'color': 'black', 'seat': 'fabric'}
Process finished with exit code 0