7-1
#coding:gbk
#7-1汽车租赁
car = input("你想租什么牌子的汽车:")
print("我给你找找有没有" + car)
7-2
#coding:gbk
#7-2餐馆订位
num_of_people = input("有多少人在用餐:")
if int(num_of_people) > 8:
print("没有空位了。")
else:
print("还有空位。")
7-3
#coding:gbk
#7-3 10的倍数
n = input("Please enter a number: ")
if int(n) % 10 == 0:
print(n + ' is a multiple of 10.')
else:
print(n + ' is not a multiple of 10.')
7-4
#coding:gbk
#7-4披萨配料
information = "请输入你想加入的配料,输入quit结束:"
a = ''
while(a != 'quit'):
a = input(information)
if a != 'quit':
print("成功添加" + a)
7-5
#coding:gbk
#7-5电影票
age = ''
while True:
age = input("请输入你的年龄(输入quit退出): ")
if age == 'quit':
break
else:
if int(age) < 3:
print("免费。")
elif int(age) <= 12:
print("票价为10美元。")
else:
print("票价为15美元.")
7-8
#coding:gbk
#7-8熟食店
sandwich_orders = ['s1', 's2', 's3']
finished_sandwiches = []
while sandwich_orders:
current_sw = sandwich_orders.pop()
print(' I made your ' + current_sw + ' sandwich')
finished_sandwiches.append(current_sw)
print(finished_sandwiches)
8-1
#coding:gbk
#8-1 消息
def display_message():
print("本章学习的是函数相关的内容")
display_message()
8-2
#coding:gbk
#8-2 喜欢的图书
def favorite_book(title):
print(" One of my favorite books is " + title)
favorite_book('Alice in Wonderland')
8-3
#coding:gbk
#8-3 T恤
def make_shirt(size, word = 'I love Python'):
#尺码有小码S,中码M,大码L
print('这件T恤的尺码是' + size + ', 需要打印的字样是:' + word)
make_shirt('M', 'J love T')
8-4
#coding:gbk
#8-4 大号T恤
def make_shirt(size = 'L', word = 'I love Python'):
#尺码有小码S,中码M,大码L
print('这件T恤的尺码是' + size + ', 需要打印的字样是:' + word)
make_shirt()
make_shirt('M')
make_shirt(word = 'J love T')
8-5
#coding:gbk
#8-5 城市
def describe_city(name, country = 'China',):
print(name + 'is in ' + country)
describe_city('Shanghai')
describe_city('Dandong')
describe_city('NewYork', 'America')
8-6
#coding:gbk
#8-6城市名
def city_country(city_name, country):
s = '"' + city_name + ', ' + country + '"'
return s
s1 = city_country('Shanghai', 'China')
print(s1)
8-7~8-8
#coding:gbk
#8-7 专辑
def make_album(singer, album_name):
a = {'singer':singer, 'album_name':album_name}
return a
a = make_album('Timmy', 'Light')
print(a)
b = make_album('Timmy', '15Minute')
print(b)
c = make_album('Timmy', 'Time')
print(c)
#8-8 用户的专辑
while(True):
s1 = input("请输入歌手姓名(输入‘quit’结束程序): ")
if s1 == 'quit':
break;
s2 = input("请输入专辑名称(输入‘quit’结束程序): ")
if s2 == 'quit':
break;
print(make_album(s1, s2))
8-9~8-11
#coding:gbk
#8-9 魔术师
def show_magicians(magicians):
for magician in magicians:
print(magician)
magicians = ['Johnny', 'Tom', 'Jerry', 'Timmy']
show_magicians(magicians)
print('--------------------------------------\n')
#8-10 了不起的魔术师
def make_great(magicians):
length = len(magicians)
for a in range(0, length):
magicians[a] = 'the Great ' + magicians[a]
return magicians
'''
【遇到的问题】
def make_great(magicians):
for a in magicians: #这样只是修改了副本a,没有修改列表里的元素
a = 'the Great ' + a
print(a)
'''
make_great(magicians)
show_magicians(magicians)
print('--------------------------------------\n')
#8-11 不变的魔术师
great_magi = make_great(magicians[:])
show_magicians(great_magi)
show_magicians(magicians)
8-12
#coding:gbk
#8-12 三明治
def make_san(*materials):
print("Making a sandwich with the following materials:")
for material in materials:
print('-' + material)
make_san('a', 'b', 'c')
make_san('d', 'e')
make_san('k')
8-13
#coding:gbk
#8-13 用户简介
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('Lily', 'G',
location='princeton',
field='physics')
print(user_profile)
8-14
#coding:gbk
#8-14 汽车
def make_car(manufac, ver, **info):
car = {}
car['manufacturer'] = manufac
car['version'] = ver
for key, value in info.items():
car[key] = value
return car
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)