如“Let me see if I can find you a Subaru”。
car=input("Which brand of cars would you like to rent: ")
print("Let me see if I can find you a "+car)
条消息,指出没有空桌;否则指出有空桌。
number=input("How many clients? ")
number=int(number)
if number>8:
print("Sorry, there is no enough table.")
else:
print("Welcome!")
7-3 10 的整数倍:让用户输入一个数字,并指出这个数字是否是10 的整数倍。
number=input()
number=int(number)
if number%10==0:
print("Yes")
else:
print("No")
7-5 电影票:有家电影院根据观众的年龄收取不同的票价:不到3 岁的观众免费;
3~12 岁的观众为10 美元;超过12 岁的观众为15 美元。请编写一个循环,在其中询问
用户的年龄,并指出其票价。
7-6 三个出口:以另一种方式完成练习7-4 或练习7-5,在程序中采取如下所有做法。
在while 循环中使用条件测试来结束循环。
使用变量active 来控制循环结束的时机。
使用break 语句在用户输入'quit'时退出循环
active=True
while active:
age=input("age: ")
if age=="quit": active=False
else:
age=int(age)
if age<3:print("free")
elif age<=12:print("$10")
else:print("$15")
7-8 熟食店:创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名
字;再创建一个名为finished_sandwiches 的空列表。遍历列表sandwich_orders,对于
其中的每种三明治,都打印一条消息,如I made your tuna sandwich,并将其移到列表
finished_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
7-9 五香烟熏牛肉(pastrami)卖完了:使用为完成练习7-8 而创建的列表
sandwich_orders,并确保'pastrami'在其中至少出现了三次。在程序开头附近添加这样
的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将
列表sandwich_orders 中的'pastrami'都删除。确认最终的列表finished_sandwiches 中
不包含'pastrami'。
sandwich_orders=["pastrami","pork","beef","pastrami","pastrami"]
finished_sandwiches=[]
print("The pastramin has been sold out.")
while "pastrami" in sandwich_orders:
sandwich_orders.remove("pastrami")
while sandwich_orders:
current_sandwich=sandwich_orders.pop()
finished_sandwiches.append(current_sandwich)
if "pastrami" in finished_sandwiches: print("error")
else: print("OK")
for sandwich in finished_sandwiches:
print(sandwich)
7-10 梦想的度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于“If
you could visit one place in the world, where would you go?”的提示,并编写一个打印调
查结果的代码块。
active=True
place={}
while active:
name=input("What's your name: ")
if name=="quit": active=False
else:
p=input("If you could visit one place in the world, where would you go? ")
place[name]=p
for x,y in place.items():
print(x+" would go to "+y)
8-2 喜欢的图书:编写一个名为favorite_book()的函数,其中包含一个名为title
的形参。这个函数打印一条消息,如One of my favorite books is Alice in Wonderland。
调用这个函数,并将一本图书的名称作为实参传递给它。
def favorite_book(title):
"""显示最喜欢的书"""
print("One of my favorite books is "+title)
favorite_book("Alice in Wonderland")
8-5 城市:编写一个名为describe_city()的函数,它接受一座城市的名字以及该城
市所属的国家。这个函数应打印一个简单的句子,如Reykjavik is in Iceland。给用
于存储国家的形参指定默认值。为三座不同的城市调用这个函数,且其中至少有一座城
市不属于默认国家。
def describe_city(city, country="China"):
print(city+" is in "+country)
describe_city("Beijing")
describe_city("Tokyo","Japan")
describe_city("New York","America")
8-7 专辑:编写一个名为make_album()的函数,它创建一个描述音乐专辑的字典。
这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函
数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑
的信息。
给函数make_album()添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用
这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。
8-8 用户的专辑:在为完成练习8-7 编写的程序中,编写一个while 循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数make_album(),并
将创建的字典打印出来。在这个while 循环中,务必要提供退出途径。
def make_album(singer, name, song_number=''):
album={"singer":singer.title(),"name":name.title()}
if song_number:album["song_number"]=int(song_number)
return album
while True:
print('You can quit anytime by inputing "quit"')
singer=input("singer: ")
if singer=='quit': break
name=input("name of the album: ")
if name=='quit': break
know=input("Do you konw the number of songs(yes/no)? ")
if know=="yes":
song_number=input("number of songs: ")
if song_number=='quit': break
elif kwon=='quit': break
print(make_album(singer,name,song_number))
8-9 魔术师: 创建一个包含魔术师名字的列表, 并将其传递给一个名为
show_magicians()的函数,这个函数打印列表中每个魔术师的名字。
8-10 了不起的魔术师:在你为完成练习8-9 而编写的程序中,编写一个名为
make_great()的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“the
Great”。调用函数show_magicians(),确认魔术师列表确实变了。
8-11 不变的魔术师:修改你为完成练习8-10 而编写的程序,在调用函数
make_great()时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后
的列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_magicians(),
确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“the
Great”的魔术师名字。
magicians=["Peter","Jack","Lily"]
def show_magicians(magicians):
new_magicians=[]
while magicians:
new_magicians.append("The great "+magicians.pop())
return new_magicians
great_magicians=show_magicians(magicians[:])
print(magicians)
print(great_magicians)