Python ().is_integer() 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17170226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:39:50  来源:igfitidea点击:

().is_integer() not working

pythonpython-2.7integer

提问by aaaa

Whats wrong with this code:

这段代码有什么问题:

n = 10
((n/3)).is_integer()

I do not understand why I cannot set n = any number and check if it is an integer or not.

我不明白为什么我不能设置 n = 任何数字并检查它是否是整数。

Thanks for your help!

谢谢你的帮助!

python 2.7.4

蟒蛇 2.7.4

error:

错误:

Traceback (most recent call last):
  File "/home/userh/Arbeitsfl?che/übung.py", line 2, in <module>
    print ((n/3)).is_integer()
AttributeError: 'int' object has no attribute 'is_integer'

回答by Ohlin

...

...

When I wrote this answer there was no information about language.

当我写这个答案时,没有关于语言的信息。

But in python2 you can use the following to check if it's an integer or not

但是在python2中,您可以使用以下内容来检查它是否是整数

isinstance( <var>, ( int, long ) )

回答by Morwenn

You are using Python 2.7. Unless you use from __future__ import division, dividing two integers will return you and integer. is_integerexists only in float, hence your error.

您正在使用 Python 2.7。除非您使用from __future__ import division,否则将两个整数相除将返回您和整数。is_integer仅存在于 中float,因此您的错误。

回答by Sebastian ?rleryd

The reason you get this error is because you divide the integer 10 by 3 using integer division, getting the integral number 3 in the form of an intinstance as a result. You then try to call the method is_integer()on that result but that method is in the floatclass and not in the intclass, just as the error message says.

出现此错误的原因是因为您使用整数除法将整数 10 除以 3,int结果以实例的形式得到整数 3 。然后,您尝试is_integer()对该结果调用该方法,但该方法在float类中而不是在int类中,正如错误消息所述。

A quick fix would be to change your code and divide by 3.0instead of 3which would result in floating point division and give you a floatinstance on which you can call the is_integer()method like you are trying to. Do this:

一个快速的解决方法是更改​​您的代码并除以除法,3.0而不是3将导致浮点除法,并为您提供一个float实例,您可以在该实例上is_integer()像尝试一样调用该方法。做这个:

n = 10
((n/3.0)).is_integer()

回答by andrew cooke

the other answers say this but aren't very clear (imho).

其他答案是这样说的,但不是很清楚(恕我直言)。

in python 2, the /sign means "integer division" when the arguments are integers. that gives you just the integer part of the result:

在 python 2 中,当参数为 integers 时/符号表示“整数除法” 。只给你结果的整数部分:

>>> 10/3
3

which means that in (10/3).is_integer()you are calling is_integer()on 3, which is an integer. and that doesn't work:

这意味着(10/3).is_integer()您正在调用is_integer()3,这是一个整数。这不起作用:

>>> (3.0).is_integer()
True
>>> (3).is_integer()
AttributeError: 'int' object has no attribute 'is_integer'

what you probably want is to change one of the numbers to a float:

您可能想要的是将其中一个数字更改为浮点数:

>>> (10/3.0).is_integer()
False

this is fixed in python 3, by the way (which is the future, and a nicer language in many small ways).

顺便说一句,这是在python 3中修复的(这是未来,并且在许多小方面是一种更好的语言)。

回答by Ravikiran Reddy Kotapati

You can use isdigit, this is a good function provided by Python itself

可以使用isdigit,这是Python本身提供的一个很好的函数

You can refer documentation here https://docs.python.org/2/library/stdtypes.html#str.isdigit

您可以在此处参考文档https://docs.python.org/2/library/stdtypes.html#str.isdigit

    if token.isdigit():
        return int(token)