7-8 使用while 循环来处理列表和字典

# -*- coding:utf-8 -*-
#li hongliang 2020年06月03日
#7.3 使用while 循环来处理列表和字典

#7-8 熟食店:创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名
#字;再创建一个名为finished_sandwiches 的空列表。遍历列表sandwich_orders,对于
#其中的每种三明治,都打印一条消息,如I made your tuna sandwich,并将其移到列表
#finished_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。

sandwich_orders = ['fruit_sandwich','Vegetables_sandwich','drumsticks_sandwich']
finished_sandwiches = []
while sandwich_orders:
    current_doing = sandwich_orders.pop()
    print('I made your '+current_doing.title())
    finished_sandwiches.append(current_doing)
print("\nAll sandwiches have been made:")
for sandwich in finished_sandwiches:
    print(sandwich.title())

#7-9 五香烟熏牛肉(pastrami)卖完了:使用为完成练习7-8 而创建的列表
#sandwich_orders,并确保'pastrami'在其中至少出现了三次。在程序开头附近添加这样
#的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将
#列表sandwich_orders 中的'pastrami'都删除。确认最终的列表finished_sandwiches 中不包含'pastrami'。

print("\nThe deli's spiced smoked beef is sold out")
sandwich_orders = ['Pastrami','fruit_sandwich','Pastrami','Vegetables_sandwich','Pastrami','drumsticks_sandwich']
print(sandwich_orders)
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?”的提示,并编写一个打印调查结果的代码块。

tourist_attraction = {}

active_answer = True
while active_answer:
    name = input("\nWhat is your name?")
    place = input('\nIf you could visit one place in the world, where would you go?')
    tourist_attraction[name]= place
    repeat= input('Would you like to let another person respond? (yes/no)')
    if repeat =='yes':
        active_answer = False
print("\n--- Poll Results ---")
for name,place in tourist_attraction.items():
    print(name+" would go "+ place)

   






你可能感兴趣的:(python学习)