这是我写的
prompt='\n本电影院分年龄段收费,请输入您的年龄:'
prompt+="\n(Enter 'quit' when you are finished)"
while True:
age=int(input(prompt))
if age<3:
print('您无需支付费用。')
elif 3<=age<=12:
print('您需要支付10美元。')
elif age>12:
print('您需要支付15美元。')
elif age=='quit':
break
else:
print("请输入阿拉伯数字。")
当我输入“二十”的时候就出错了,我是希望它能够提醒“请输入阿拉伯数字。”
报错信息:
本电影院分年龄段收费,请输入您的年龄:
(Enter 'quit' when you are finished)二十
Traceback (most recent call last):
File "D:\Python_Practice\7-5 电影票.py", line 5, in <module>
age=int(input(prompt))
ValueError: invalid literal for int() with base 10: '二十'
>>>
如果想提示:请输入阿拉伯数字,可以把这行代码放在try里面
try:
age=int(input(prompt))
except:
print("请输入阿拉伯数字!")
prompt='\n本电影院分年龄段收费,请输入您的年龄:'
prompt+="\n(Enter 'quit' when you are finished)"
while True:
age=input(prompt)
if not age.isdigit():
print("请输入阿拉伯数字。")
continue
if age<3:
print('您无需支付费用。')
elif 3<=age<=12:
print('您需要支付10美元。')
elif age>12:
print('您需要支付15美元。')
elif age=='quit':
break
我发现我添加标签搞错了,只想添加python的。sorry0.0
二十用int是没法强转的,输入20可以