0

I am trying to develop a python script that loops through a set of subdirectories I have and extracts a data file from each of those subdirectories. My subdirectories are each named for their corresponding time in a simulation I have fun earlier. However, the issue is that the integer subdirectories are listed without a decimal point (e.g. 5), while the non-integer subdirectories are listed with a decimal point. So in order to find the correct subdirectory, my loop is very sensitive to the number type.

With that, I have been trying to use the is._integer() method with python, but I keep getting this error:

'int' object has no attribute 'is_integer'

when I run the following code:

def frange(x, y, jump):
    while x < y:
        yield x
        x += jump

time=15

for i in frange(0,time,0.1):
    float(i)    
    print (i).is_integer() 

even when I am brute forcing my loop variable to be a float! Please help.

electriclady
  • 1
  • 1
  • 3

3 Answers3

1

Try print(float(i).is_integer())

The reason is that despite the fact that you casted i with float(i) before the usage of (i).is_integer(), it did not mutate the variable type of i to float.

woozyking
  • 4,880
  • 1
  • 23
  • 29
1
float(i)

This converts the argument i into a float, but that result is returned; it does not modify the variable i. So what that call does is simply discarded.

And in your case, in the first iteration, i is 0 which is an actual integer, and integer objects don’t have an is_integer method. So you either want to assign the restult of float(i) back to i:

for i in frange(0, time, 0.1):
    i = float(i)
    print (i).is_integer()

… or make sure that frange only returns floats:

def frange(x, y, jump):
    x = float(x)
    while x < y:
        yield x
        x += jump
poke
  • 369,085
  • 72
  • 557
  • 602
  • Ok, so all of these suggestions work (thank you!). However now I have made my code like this: `Code' for i in frange(0,time,0.1): x = float(i) print x print (x).is_integer() `Code' and the only value it is telling me I have an integer for is "0.0" and nothing else even though I am going up to a time of 15 with increments of 0.1. Thoughts? – electriclady Mar 24 '16 at 21:04
  • 1
    That’s because it’s floating point arithmetic, see [this question](http://stackoverflow.com/q/588004/216074). – poke Mar 24 '16 at 21:06
  • But the reason I am using .is_integer() is because I thought it worked with floating point arithmetic – electriclady Mar 24 '16 at 21:07
  • It works, but only if the number is exactly e.g. `1.00000000000000`. But that’s unlikey due to how floating point arithmetic works. – poke Mar 24 '16 at 21:08
  • Hmmm... do you have any other suggestions as to how I can do this? – electriclady Mar 24 '16 at 21:10
  • You could use module and then compare against some precision threshold, e.g. `i % 1 < 0.01` to check whether something is close to an integer. – poke Mar 24 '16 at 21:12
0

You do not assign cast value to a variable. This should work:

def frange(x, y, jump):
    while x < y:
        yield x
        x += jump

time=15

for i in frange(0,time,0.1):
    x = float(i)    
    print (x).is_integer() 
max
  • 2,757
  • 22
  • 19
  • Ok, so all of these suggestions work (thank you!). However now I have made my code like this: for i in frange(0,time,0.1): x = float(i) print x print (x).is_integer() `Code' and the only value it is telling me I have an integer for is "0.0" and nothing else even though I am going up to a time of 15 with increments of 0.1. Thoughts? – electriclady Mar 24 '16 at 21:08