Robin_0102 2021-01-30 23:43 采纳率: 0%
浏览 10

麻烦大佬们帮看看python课后习题7-5我的else是哪里错了吗?

这是我写的

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: '二十'
>>> 

  • 写回答

4条回答 默认 最新

  • Nick Peng 2021-01-31 03:46
    关注

    如果想提示:请输入阿拉伯数字,可以把这行代码放在try里面

    try:
        age=int(input(prompt))
    except:
        print("请输入阿拉伯数字!")
    评论

报告相同问题?