Pythonwhile循环电影院门票问题(Python从⼊门到实践7-5)
while循环
问题:
有家电影院根据观众年龄收取不同的票价:不到三岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写⼀个循环,在其中询问⽤户的年龄,并指出票价。
问题程序:
price = "The price of movie ticket is "
msg = ''
while msg != 'quit':
msg = input("How old are you?")
if int(msg) < 3:
print (price + "$0.")
elif 3 <= int(msg) <12:
print (price + "$10.")
elif int(msg) >= 12:
print (price + "$15.")
时间循环的电影有哪些else:
break
此程序每次当我输⼊ quit时,总会出现错误:错误在我输⼊的不是int格式
但我觉得逻辑上是没有问题,应该是因为while循环每次都会会从循环开始⽐较,所以造成quit⽆法形成int(msg),也就导致了上⾯的错误,因此,我把程序改成了先⽐较quit:
修改后程序
price = "The price of movie ticket is "
msg = ''
while msg != 'quit':
msg = input("How old are you?")
if msg == 'quit':
break
elif int(msg) < 3:
print (price + "$0.")
elif 3 <= int(msg) <12:
print (price + "$10.")
else :
print (price + "$15.")
如此修改后,在输⼊quit后就没有错误了:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。