Python编程从入门到实践:习题7-8~7-10

来源:互联网 发布:环球英语怎么样知乎 编辑:程序博客网 时间:2024/05/22 02:27
#7-8 熟食店 :创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;#再创建一个名为finished_sandwiches 的空列表。#遍历列表sandwich_orders ,对于其中的每种三明治,都打印一条消息,#如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。#所有三明治都制作好后,打印一条消息,将这些三明治列出来。sandwich_orders = ['奶油三明治','沙拉三明治','黄油三明治']finished_sandwiches = []for sandwich in sandwich_orders:    print('已经开始制作',sandwich)    finished_sandwiches.append(sandwich)print('\n已经全部做好!')for sandwich in finished_sandwiches:    print(sandwich)#7-9 五香烟熏牛肉(pastrami)卖完了 :使用为完成练习7-8而创建的列表sandwich_orders ,#并确保'pastrami' 在其中至少出现了三次。#在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;#再使用一个while 循环将列表sandwich_orders 中的'pastrami' 都删除。#确认最终的列表finished_sandwiches 中不包含'pastrami' 。sandwich_orders = ['奶油三明治','pastrami','沙拉三明治','黄油三明治','pastrami','pastrami']print()print(sandwich_orders)print('pastrami已经卖完!')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?”的提示,并编写一个打印调查结果的代码块。vacation_spot = {}polling_active = Truewhile polling_active:    name = input('请输入您的姓名:')    place = input('If you could visit one place in the world, where would you go?')    vacation_spot[name] = place    repeat = input("Would you like to let another person respond? (yes/ no) ")    if repeat == 'no':        polling_active = Falseprint("\n--- Poll Results ---")for name, place in vacation_spot.items():    print(name + " would like to visit " + place + ".")

阅读全文
0 0
原创粉丝点击