《Python从入门到实践》--第七章 用户输入和while循环 课后练习
题目:
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' 。
7-10 梦梦想想的的度度假假胜胜地地 :编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one placein the world, where would you go?”的提示,并编写一个打印调查
结果的代码块。
代码:
#!usr/bin/python
# _*_ coding:utf-8 _*_
'''
#熟食店
sandwich_orders = ["tomato_sandwich","beef_sandwich","onion_sandwich"]
finished_sandwiches = []
#当sandwich_orders 的列表不为空时,循环一直执行
while sandwich_orders:
for sandwich_order in sandwich_orders:
print("我给你做了%s"%sandwich_order)
if sandwich_order in sandwich_orders:
finished_sandwich = sandwich_orders.pop()
finished_sandwiches.append(finished_sandwich)
print("下面是制作好的三明治:")
for finished_sandwich2 in finished_sandwiches:
print(finished_sandwich2)
'''
#五香烟熏牛肉
sandwich_orders2 = [
"tomato_sandwich",
"beef_sandwich",
"onion_sandwich",
"pastrami",
"pastrami",
"pastrami",
]
while "pastrami" in sandwich_orders2:
sandwich_orders2.remove("pastrami")
print("尊敬的各位顾客,本店今天的五香牛肉卖完了")
print("本店还剩下列品种的三明治可供选择:")
for sandwich_selling in sandwich_orders2:
print(sandwich_selling)
#梦想的度假圣地
places = {}
polling_active = True
while polling_active:
name = input("请输入你的名字:")
place = input("请输入你想去的地方")
places[name] = place
another_case = input("还有人想参与调查么:yes/no")
if another_case == "no":
polling_active = False
print("下面是调查的结果")
for place2 in places:
print(place2)

浙公网安备 33010602011771号