count = int(input("input your count:")) 我输入非整数就会报错,如何解决这种问题啊,刚开始学,不懂

baidu_20119467 2014-08-27 05:35:04
count = int(input("input your count:"))
我输入非整数就会报错
ValueError: invalid literal for int() with base 10: ''
这种情况如何解决啊,还没学到异常处理,求解
...全文
445 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
神奕 2014-08-28
  • 打赏
  • 举报
回复
int(x) 参数x不是数字字面值会报错
  • 打赏
  • 举报
回复
如果你完全不想要浮点数 那么就改良一下 count = None s=raw_input('input your count: ') while not s.isdigit() and count == None: s=raw_input('not a number, please try again: ') if abs(int(float(s))) < abs(float(s)): #比较绝对值,可以区分负整数和负的浮点数 continue count = int(float(s)) print count #----------- 如果只需要非负整数 那么while里改成

while not s.isdigit() and count == None:
    s=raw_input('not a number, please try again: ')
    if  float(s) < 0:
        continue
    if abs(int(float(s))) < abs(float(s)):  #比较绝对值,可以区分负整数和负的浮点数
        continue
    count = int(float(s))
  • 打赏
  • 举报
回复
引用 5 楼 baidu_20119467 的回复:
[quote=引用 2 楼 xmnathan 的回复:] 非整数强转int 自然会报错 你可以加try 然后再捕获到异常的时候另外处理一下
恩,不会用try catch,没找到例子。求指点,如何写这个异常处理。[/quote] 6楼的写法就可以了 except: 后面你可以根据具体用法自己看要怎么处理 ------------------ 另外你也可以这么处理

s=raw_input('input your count: ')
while not s.isdigit() :#判定输入的是否是数字(整数或者浮点)
    s=raw_input('not a number, please try again: ')
#数字先转浮点再转count取整数部分,可以去掉浮点的小数部分
count = int(float(s))
------------------
baidu_20119467 2014-08-28
  • 打赏
  • 举报
回复
引用 8 楼 xmnathan 的回复:
如果你完全不想要浮点数 那么就改良一下 count = None s=raw_input('input your count: ') while not s.isdigit() and count == None: s=raw_input('not a number, please try again: ') if abs(int(float(s))) < abs(float(s)): #比较绝对值,可以区分负整数和负的浮点数 continue count = int(float(s)) print count #----------- 如果只需要非负整数 那么while里改成

while not s.isdigit() and count == None:
    s=raw_input('not a number, please try again: ')
    if  float(s) < 0:
        continue
    if abs(int(float(s))) < abs(float(s)):  #比较绝对值,可以区分负整数和负的浮点数
        continue
    count = int(float(s))
多谢多谢,我再研究研究。isdigit不知道是干啥的。我摆个度去
baidu_20119467 2014-08-28
  • 打赏
  • 举报
回复
引用 6 楼 u013171165 的回复:
tag = 0
while tag == 0: 
    try:
        count  = int(input("input your count:"))
        tag = 1 
    except:
        print("不是整数")
多谢多谢,帮我捅破了这层窗户纸
The_Third_Wave 2014-08-27
  • 打赏
  • 举报
回复
tag = 0
while tag == 0: 
    try:
        count  = int(input("input your count:"))
        tag = 1 
    except:
        print("不是整数")
baidu_20119467 2014-08-27
  • 打赏
  • 举报
回复
引用 2 楼 xmnathan 的回复:
非整数强转int 自然会报错 你可以加try 然后再捕获到异常的时候另外处理一下
恩,不会用try catch,没找到例子。求指点,如何写这个异常处理。
baidu_20119467 2014-08-27
  • 打赏
  • 举报
回复
引用 1 楼 u013171165 的回复:
关键是你想干什么?这本来叫你输入的就是整数,你干嘛给自己找麻烦?
就是这种异常如何处理,我知道本意是让我输入整数,如果输入非整数,需要如何处理? try catch 没找到例子,不会用。
The_Third_Wave 2014-08-27
  • 打赏
  • 举报
回复
引用 1 楼 u013171165 的回复:
关键是你想干什么?这本来叫你输入的就是整数,你干嘛给自己找麻烦?
1、Py2没问题; 2、你用的是Py3,如果想要浮点数,你可以count = float(input("input your count:"))
  • 打赏
  • 举报
回复
非整数强转int 自然会报错 你可以加try 然后再捕获到异常的时候另外处理一下
The_Third_Wave 2014-08-27
  • 打赏
  • 举报
回复
关键是你想干什么?这本来叫你输入的就是整数,你干嘛给自己找麻烦?

37,717

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧