高级编程技术(Python)作业7

书上写了sublime无法运行用户交互的代码,事实上只要安装一个REPL就可以进行用户交互了。但是REPL并没有办法进行死循环的跳出处理,所以一旦代码出现死循环,sublime就会失去响应,只能关闭sublime。而不使用REPL正常运行python就可以使用Ctrl + C中断死循环但是又不能进行用户交互。因此如果要用sublime编写大规模的代码并运行,最好还是在cmd的环境下运行。

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

Solution:

sandwich_orders = ["hotdog", "salad", "tuna"]
finished_sandwiches = []
while(sandwich_orders):
    sandwich = sandwich_orders.pop()
    print("I made your " + sandwich +" sandwich.")
    finished_sandwiches.append(sandwich)

print("\nThese are sandwiches finished.")
for finished_sandwich in finished_sandwiches:
    print("\t" + finished_sandwich.title() + " sandwich")

Output:

I made your tuna sandwich.
I made your salad sandwich.
I made your hotdog sandwich.

These are sandwiches finished.
    Tuna sandwich
    Salad sandwich
    Hotdog sandwich

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

Solution:

sandwich_orders = ["hotdog", "pastrami", "salad", 
                   "pastrami", "tuna", "pastrami"]
finished_sandwiches = []

print("All the pastrami is sold out!\n")
while "pastrami" in sandwich_orders:
    sandwich_orders.remove("pastrami")

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

print("\nThese are sandwiches finished.")
for finished_sandwich in finished_sandwiches:
    print("\t" + finished_sandwich.title() + " sandwich")

if "pastrami" in finished_sandwiches:
    print("\nThere must be something wrong.")
else:
    print("\nThere are no pastrami sandwiches.")

Output:

All the pastrami is sold out!

I made your tuna sandwich.
I made your salad sandwich.
I made your hotdog sandwich.

These are sandwiches finished.
    Tuna sandwich
    Salad sandwich
    Hotdog sandwich

There are no pastrami sandwiches.

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

Solution:

polling_active = True
responses = {}

while polling_active:
    name = input("What is your name? ")
    sentence = "If you could visit one place in the world,"
    sentence += "\nwhere would you go ? "
    place = input(sentence)
    responses[name] = place

    while True:
        repeat = input("Finished? Y/N\n")
        if (repeat == "Y"):
            polling_active = False
            break
        elif(repeat == "N"):
            print("Next one.\n")
            break
        else:
            print("Wrong input. Please input again.\n")

print("This is the result.")
for name, place in responses.items():
    print(name.title() + " would like to " + place.title() + ".")

Output:

What is your name? tim
If you could visit one place in the world,
where would you go ? tokyo
Finished? Y/N
N
Next one.

What is your name? ashero
If you could visit one place in the world,
where would you go ? paris
Finished? Y/N
djd
Wrong input. Please input again.

Finished? Y/N
Y
This is the result.
Tim would like to Tokyo.
Ashero would like to Paris.

注:书上的实例没有判断用户如果乱输入的情况,实际问题中应该考虑这个问题。flag active的主要用途就是用来处理循环中套循环的跳出判断

猜你喜欢

转载自blog.csdn.net/weixin_38311046/article/details/79780005