Chapter7:用户输入和while循环习题选做

7-2 餐馆订位 :编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。

代码如下:

num = input("The number of people who will eat here is:")
num = int(num)
if num >= 8:
    print("Do not have enough tables!")
else:
    print("Have enough tables!")

7-5 电影票 :有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。

代码如下

while(1):
    age = input("What's your age? ")
    age = int(age)
    if age<3:
        print("The ticket is free.")
    elif age>12:
        print("The price is $15")
    else:
        print("The price is $10")

7-7 无限循环 :编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl +C,也可关闭显示输出的窗口)。

代码如下:

while1):
    print("I'm a dead loop.")

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

代码如下:

sandwich_orders = ["tomato_sandwich","beef_sandwich","potato_sandwich","tuna_sandwich"]
finished_sandwiches = []
for sandwich in sandwich_orders:
    print("I made tour "+ sandwich)
    finished_sandwiches.append(sandwich)
print(finished_sandwiches)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值