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

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

题目:电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费; 3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,指出其票价。
程序1为(使用 break 语句在用户输入 ‘quit’ 时退出循环):

prompt = "\nPlease enter the your age:"
prompt += "\n(Enter 'quit' when you are finished.) "

while True :
  age = input(prompt)
  if age == 'quit':
    break
  else:
      age=int(age)
      if age<3:
          print("free")
      elif (age>=3 and age<=12):
          print("The ticket price is 10 dollars.")
      else:
          print("The ticket price is 15 dollars.")

程序2为(使用变量 active 来控制循环结束的时机):

prompt = "\nPlease enter the your age:"
prompt += "\n(Enter 'quit' when you are finished.) "
active=True
while active :
  age = input(prompt)
  if age == 'quit':
    active=False
  else:
      age=int(age)
      if age<3:
          print("free")
      elif (age>=3 and age<=12):
          print("The ticket price is 10 dollars.")
      else:
          print("The ticket price is 15 dollars.")

输入2时,结果为:

Please enter the your age:
(Enter 'quit' when you are finished.) 2
free

输入5时,结果为:

Please enter the your age:
(Enter 'quit' when you are finished.) 5
The ticket price is 10 dollars.

输入45时,结果为:

Please enter the your age:
(Enter 'quit' when you are finished.) 45
The ticket price is 15 dollars.

输入‘quit’时(终止程序),结果为:

Please enter the your age:
(Enter 'quit' when you are finished.) quit

Process finished with exit code 0

  • 20
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值