鱼C论坛

 找回密码
 立即注册
查看: 1947|回复: 9

[已解决]Python中int()报错

[复制链接]
发表于 2017-5-8 11:00:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
今天刚看了小甲鱼的课,看到数据类型那一讲,尝试了一下
>>> a = 5.99
>>> b = int(a)
>>> print(b)
5


在IDLE中,直接输入没有问题
然后我新建了一个

a = input('请输入一个数字:')
b = int(a)
print(b)


请输入一个数字:5.99
Traceback (most recent call last):
  File "E:/1.py", line 2, in <module>
    b = int(a)
ValueError: invalid literal for int() with base 10: '5.99'

>>>

输入数字为整数的话,没有问题,想知道,为什么输入小数,会报错。
萌新一枚,谢谢各位大佬的帮助。
最佳答案
2017-5-8 16:40:39
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).


看说明文档,红字处说明了,如果输入不是数字类型的,那么那个变量代表的必须是一个整数

对于你所举的例子,
第一个 a = 5.99  a是数字类型(浮点型),所以那么转换没有问题
第二个 a = input('请输入一个数字:') 这个得到的是个字符类型,但这个字符又不是代表整数的字符,所以就会报错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-8 11:21:47 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-8 11:31:10 | 显示全部楼层
这要先float(input)
在int()才行
感觉这个设定好不科学。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-8 11:46:09 | 显示全部楼层
ooxx7788 发表于 2017-5-8 11:31
这要先float(input)
在int()才行
感觉这个设定好不科学。

谢谢0.0
尝试了一下,问题解决了
但是道理不是很懂。
为什么前一个可以直接int()呢= =
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-8 11:54:13 | 显示全部楼层
浮点型和整数的字符型可以用int
而小数的字符型不能用int
至于为什么,我说不好。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-8 12:35:33 | 显示全部楼层
之前可以是因为你转换的a是浮点型数据,然后下一个a你是用input方法获得的,input方法会返回字符串类型的值,所以此时a为字符串类型,python的int方法支持转换纯整数的字符串,但如果你转换为浮点型的字符串就有问题了,比如‘5.99’在字符串里面有一个‘.’,python中的int方法大概没有对字符串中的'.'做特殊处理,只是将其看作是一个字符,所以int('5.99')的感觉就像是int('5a99'),以上只是我的个人猜测

评分

参与人数 1荣誉 +5 收起 理由
丿小米团 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2017-5-8 16:40:39 | 显示全部楼层    本楼为最佳答案   
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).


看说明文档,红字处说明了,如果输入不是数字类型的,那么那个变量代表的必须是一个整数

对于你所举的例子,
第一个 a = 5.99  a是数字类型(浮点型),所以那么转换没有问题
第二个 a = input('请输入一个数字:') 这个得到的是个字符类型,但这个字符又不是代表整数的字符,所以就会报错了

评分

参与人数 1荣誉 +5 收起 理由
丿小米团 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-5-8 17:17:45 | 显示全部楼层
18813034116 发表于 2017-5-8 12:35
之前可以是因为你转换的a是浮点型数据,然后下一个a你是用input方法获得的,input方法会返回字符串类型的值 ...

非常感谢0.0
你说的基本都是对的
你也可以看看楼下,他比较有理有据。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-8 17:19:22 | 显示全部楼层
shuofxz 发表于 2017-5-8 16:40
看说明文档,红字处说明了,如果输入不是数字类型的,那么那个变量代表的必须是一个整数

对于你所举 ...

谢谢大佬0.0
我明白了~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-8 19:15:16 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-23 20:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表