Python编程 从入门到实践 第七章习题

7-2 餐馆订位:

number = input("How many of you are there for dinner?")
number = int(number)

if number > 8:
    print("There is no empty table。")
else:
    print("The table is available.")

输出:

How many of you are there for dinner?3
The table is available.
7-4 比萨原料
message = "需要添加什么配料:"
message += "\n输入'quit'完成点餐。"

flag = True;
while flag:
    food = input(message)

    if food == 'quit':
        flag = False
    else:
        print(food+"\n")

输出:

需要添加什么配料:
输入'quit'完成点餐。beel
beel

需要添加什么配料:
输入'quit'完成点餐。pork
pork

需要添加什么配料:
输入'quit'完成点餐。quit

7-6 三个出口:

~用while循环使用条件测试结束循环

message = "需要添加什么配料:"
message += "\n输入'quit'完成点餐。"

food = ""
while food != 'quit':
    food = input(message)

    if food != 'quit':
        print(food+"\n")

~用active控制循环结束时机

message = "需要添加什么配料:"
message += "\n输入'quit'完成点餐。"

active = True;
while active:
    food = input(message)

    if food == 'quit':
        active = False
    else:
        print(food+"\n")

~用break语句结束循环

message = "需要添加什么配料:"
message += "\n输入'quit'完成点餐。"

while True:
    food = input(message)

    if food == 'quit':
        break
    else:
        print(food+"\n")

输出都为:

需要添加什么配料:
输入'quit'完成点餐。beel
beel

需要添加什么配料:
输入'quit'完成点餐。pork
pork

需要添加什么配料:
输入'quit'完成点餐。quit

7-8 熟食店:

sandwich_orders = ['tuna', 'pork','beel']
finished_sandwiches = []

while sandwich_orders:
    finished_sandwiche = sandwich_orders.pop()

    print("I made your " + finished_sandwiche.title() + " sandwiches.")
    finished_sandwiches.append(finished_sandwiche)

print("\nThe following sandwiches have been finished:")
for sandwich in finished_sandwiches:
    print(sandwich.title())

输出:

I made your Beel sandwiches.
I made your Pork sandwiches.
I made your Tuna sandwiches.

The following sandwiches have been finished:
Beel
Pork
Tuna

7-9 pastrami 卖完了:

sandwich_orders = ['pastrami','tuna', 'pork','pastrami','beel','pastrami',]
finished_sandwiches = []

print("The former sandwiches order:")
print(sandwich_orders)

print("\nNow, Pastrami sandwich have sold out!")
while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')

print("\nThe present sandwiches order:")
print(sandwich_orders)

输出:

The former sandwiches order:
['pastrami', 'tuna', 'pork', 'pastrami', 'beel', 'pastrami']

Now, Pastrami sandwich have sold out!

The present sandwiches order:
['tuna', 'pork', 'beel']




猜你喜欢

转载自blog.csdn.net/sysu_alex/article/details/79709331