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

来源:互联网 发布:雅米网络兼职靠谱吗 编辑:程序博客网 时间:2024/06/08 06:16

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 = Truewhile 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 = Falsefor 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.

原创粉丝点击