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

查看:42
本文介绍了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 是浮动的点,转换截断为零.如果论据是在整数范围之外,函数返回一个长对象.

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 不是一个数字或者如果给出了基数,那么 x 必须是一个字符串或者Unicode 对象,表示以基数为基础的整数文字.可选地,文字可以在前面加上 + 或 - (中间没有空格之间)并被空格包围.一个 base-n 文字由数字 0 到 n-1,其中 a 到 z(或 A 到 Z)的值是 10 到 35.默认基数为 10.允许的值为 0 和 2-36.基数 2,-8,和 -16 字面值可以选择以 0b/0B、0o/0O/0 或0x/0X,与代码中的整数文字一样.Base 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天全站免登陆