Python 2.7和3.3.2,为什么int('0.0')不起作用? [英] Python 2.7 and 3.3.2, why int('0.0') does not work?

查看:370
本文介绍了Python 2.7和3.3.2,为什么int('0.0')不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,在Python中(我在2.7和3.3.2中尝试过),为什么 int('0.0')不起作用?它给出了这个错误:

As the title says, in Python (I tried in 2.7 and 3.3.2), why int('0.0') does not work? It gives this error:

ValueError: invalid literal for int() with base 10: '0.0'

如果您尝试 int('0') int(eval('0.0'))它有效......

If you try int('0') or int(eval('0.0')) it works...

推荐答案

仅仅因为 0.0 不是基数10的有效整数。而 0 是。

Simply because 0.0 is not a valid integer of base 10. While 0 is.

阅读关于 int() 这里。


int(x,base = 10)

int(x, base=10)

将数字或字符串x转换为整数,如果没有给出参数,则返回
0。如果x是数字,则它可以是普通的
整数,长整数或浮点数。如果x浮动
点,则转换将截断为零。如果参数在整数范围之外是
,则函数返回一个long对象。

Convert a number or string x to an integer, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

如果x不是数字或者给定了base,则x必须是一个字符串或
Unicode对象,表示基数的整数文字。
可选地,文字可以在前面加+或 - (在
之间没有空格)并且用空格包围。 base-n文字由
组成,数字为0到n-1,a到z(或A到Z)的值为10到35.
默认基数为10.允许的值为0和2-36。 Base-2,-8,
和-16文字可以选择前缀为0b / 0B,0o / 0O / 0或
0x / 0X,与代码中的整数文字一样。基数0表示将
字符串完全解释为整数文字,因此实际基数为2,8,
10或16。

If x is not a number or if base is given, then x must be a string or Unicode object 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/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.

这篇关于Python 2.7和3.3.2,为什么int('0.0')不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆