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.

1
  • and print(i.is_integer) , not print(i).is_integer
    – chapelo
    Commented Mar 24, 2016 at 20:24

3 Answers 3

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.

0
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
6
  • 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? Commented Mar 24, 2016 at 21:04
  • 1
    That’s because it’s floating point arithmetic, see this question.
    – poke
    Commented Mar 24, 2016 at 21:06
  • But the reason I am using .is_integer() is because I thought it worked with floating point arithmetic Commented Mar 24, 2016 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
    Commented Mar 24, 2016 at 21:08
  • Hmmm... do you have any other suggestions as to how I can do this? Commented Mar 24, 2016 at 21:10
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() 
1
  • 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? Commented Mar 24, 2016 at 21:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.