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']




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值