《Python编程:从入门到实践》第七章7.3节课后作业

    科技2022-07-17  111

    本文代码是在jupyter中实现的,仅为了自我督促学习python之用。 7-8 熟食店:创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为 finished_sandwiches 的空列表。遍历列表 sandwich_orders ,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明治都制作好后,打印一条消息,将这些三明治列出来。

    代码:

    sandwich_orders = ['Bocadillo', 'Arepa', 'Croque Madame', 'Smoked Meat'] finished_sandwiches = [] while sandwich_orders: current_sandwich = sandwich_orders.pop() print("I made your " + current_sandwich.title() + ' sandwich.') finished_sandwiches.append(current_sandwich) for finished_sandwich in finished_sandwiches: # 依次打印列表 finished_sandwiches 中的元素 print(finished_sandwich.title())

    运行结果:

    I made your Smoked Meat sandwich. I made your Croque Madame sandwich. I made your Arepa sandwich. I made your Bocadillo sandwich. Smoked Meat Croque Madame Arepa Bocadillo

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

    代码:

    sandwich_orders = ['Bocadillo', 'pastrami', 'Arepa', 'pastrami', 'Croque Madame', 'Smoked Meat', 'pastrami'] print(sandwich_orders) while 'pastrami' in sandwich_orders: # 多次训练,将列表sandwich_orders中的元素pastrami删除 sandwich_orders.remove('pastrami') print("Sorry, the pastrami has been sold out.") print(sandwich_orders) finished_sandwiches = [] while sandwich_orders: current_sandwich = sandwich_orders.pop() print("I made your " + current_sandwich.title() + ' sandwich.') finished_sandwiches.append(current_sandwich) for finished_sandwich in finished_sandwiches: # 依次打印列表 finished_sandwiches 中的元素 print(finished_sandwich.title())

    运行结果:

    ['Bocadillo', 'pastrami', 'Arepa', 'pastrami', 'Croque Madame', 'Smoked Meat', 'pastrami'] Sorry, the pastrami has been sold out. ['Bocadillo', 'Arepa', 'Croque Madame', 'Smoked Meat'] I made your Smoked Meat sandwich. I made your Croque Madame sandwich. I made your Arepa sandwich. I made your Bocadillo sandwich. Smoked Meat Croque Madame Arepa Bocadillo

    7-10 梦想的度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查结果的代码块。

    代码:

    Tourist_places = {} polling_active = True while polling_active: name = input("\nWhat is your name?") Tourist_place = input("If you could visit one place in the world, where would you go?") Tourist_places[name] = Tourist_place # 将旅行地存储在字典中 repeat = input("Would you like to let another person respond ?(yes/no)") if repeat == 'no': polling_active = False print("\n--- Poll Results ---") for name,Tourist_place in Tourist_places.items(): print(name + " would like to travel " + Tourist_place + ".")

    运行结果:

    What is your name?Li ming If you could visit one place in the world, where would you go?Bali Would you like to let another person respond ?(yes/no)yes What is your name?Zhang hua If you could visit one place in the world, where would you go?Iceland Would you like to let another person respond ?(yes/no)yes What is your name?Wang shu If you could visit one place in the world, where would you go?Paris Would you like to let another person respond ?(yes/no)no --- Poll Results --- Li ming would like to travel Bali. Zhang hua would like to travel Iceland. Wang shu would like to travel Paris.
    Processed: 0.012, SQL: 8