您的位置:首页 > 编程语言 > Python开发

Python 从入门到实践 7-8 课后习题

2017-12-14 21:16 756 查看
7.8

熟食店:创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名

字;再创建一个名为finished_sandwiches 的空列表。遍历列表sandwich_orders,对于

其中的每种三明治,都打印一条消息,如I made your tuna sandwich,并将其移到列表

finished_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。

sandwich_orders = ['Pice','Noy','Pop']
finished_sandwichers=[]
while sandwich_orders:
finished_sandwicher = sandwich_orders.pop()
print("I made your " + str(finished_sandwicher) +" sandwich.")
finished_sandwichers.append(finished_sandwicher)

print(str(finished_sandwichers))
for finished_sandwicher in finished_sandwichers:
print(finished_sandwicher.title())

7.9五香烟熏牛肉(pastrami)卖完了:使用为完成练习7-8 而创建的列表

sandwich_orders,并确保'pastrami'在其中至少出现了三次。在程序开头附近添加这样

的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将

列表sandwich_orders 中的'pastrami'都删除。确认最终的列表finished_sandwiches 中

不包含'pastrami'。

sandwich_orders = ['tofu','Pice','pastrami','Noy','pastrami','Pop','pastrami']
print("The pastrami has been sold.")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print(sandwich_orders)

7.10梦想的度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于“If

you could visit one place in the world, where would you go?”的提示,并编写一个打印调

查结果的代码块。

place=[]
print("If you could visit one place in the world, where would you go?")
#设置标志位
polling_active = True
while polling_active:
new_place = input("\n Please input the place?")
place.append(new_place)
repeat = input("Would you want to input another place?(Y/N)")
if repeat == 'N':
polling_active = False
for i in place:
print("The " + i + " you want to go.")


问题: 7.10输出时候,最后刚开始用的直接打印,代码为(print("The " + str(place) " + " you want to go. "))

输出为 The ['Sydney']  you want to go.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: