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

# 7-8
sandwich_orders = ['tuna', 'butter', 'salad']
finished_sandwiches = []

while sandwich_orders:
    finished_sandwiche = sandwich_orders.pop()
    print("I made your " + finished_sandwiche + " sandwich.")

    finished_sandwiches.append(finished_sandwiche)

for finished_sandwiche in finished_sandwiches:
    print("\t" + finished_sandwiche.title() + " sandwich")

# 7-9
sandwich_orders = ['pastrami', 'tuna', 'pastrami', 'butter', 'salad', 'pastrami']
finished_sandwiches = []

print("\nDeli's five-cigarette smoked beef sold out!")

while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')

while sandwich_orders:
    finished_sandwiche = sandwich_orders.pop()
    print("I made your " + finished_sandwiche + " sandwich.")

    finished_sandwiches.append(finished_sandwiche)

for finished_sandwiche in finished_sandwiches:
    print("\t" + finished_sandwiche.title() + " sandwich")

# 7-10
name_places = {}
flag = True

while flag:
    name = input("What's your name? ")
    place = input("If you could visit one place in the world, where would you go? ")
    name_places[name] = place
    repeat = input("Any other place?(yes/no) ")
    if repeat == 'no':
        flag = False

for name, place in name_places.items():
    print(name + " want to go to " + place.title() + ".")
I made your salad sandwich.
I made your butter sandwich.
I made your tuna sandwich.
    Salad sandwich
    Butter sandwich
    Tuna sandwich

Deli's five-cigarette smoked beef sold out!
I made your salad sandwich.
I made your butter sandwich.
I made your tuna sandwich.
    Salad sandwich
    Butter sandwich
    Tuna sandwich
What's your name? jiang
If you could visit one place in the world, where would you go? huangshan
Any other place?(yes/no) yes
What's your name? yang
If you could visit one place in the world, where would you go? 泰山
Any other place?(yes/no) no
jiang want to go to Huangshan.
yang want to go to 泰山.
    原文作者:庵下桃花仙
    原文地址: https://www.jianshu.com/p/f1b182c0dc45
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞