3

Below is my code for a problem on leetcode:

for i in range(len(nums)):
    ans =0   
    ans+=nums[i]
    for j in range(i+1,len(nums)):
        realans=0
        ans+=nums[j]
        realans= ans/k
        if isinstance(realans,int):
            return True
    return False

I want to check if the variable realans is an integer or not upon division. But even if it is an integer it takes the value of 7.0 ex: 14/2 = 7.0

If I add // instead of /, for the values where I get 7.3 it converts to 7 making the logic wrong. How can I fix this? Just to make sure, I don't want the number to be rounded off.

Tom Brooke
  • 171
  • 3
  • 15
  • 3
    This is an xy problem — you don't actually care about the value of `realans`. Don't check for whether `realans` is an integer. Check `if ans % k == 0:`, which will be true when `k` divides `ans` evenly. – Mark Jun 09 '22 at 22:50
  • `int` is a data type that can only represent *integral numbers* i.e. integers. `float` is another data type, which can represent rational numbers, including integral numbers. If you want to check if a `float` object represents an integer, use `my_float.is_integer()` But as stated above, you should be using another approach entirely – juanpa.arrivillaga Jun 09 '22 at 22:52

0 Answers0