Song
Song

Reputation: 328

math.sqrt results in TypeError:'float' object cannot be interpreted as an integer

I'm doing a homework assignment and want to fully train all the classes to what they are expected to do. I came across this TypeError which was really bugging me around.

This is for a little kids' test of health evaluation. I've tried google, but that just gave me one big blob of code, and I've tried deleting the Power class and of course, gave me an error.

The main loop(not finished):

pd = Power()
num = pd.calc_num()
cir_num = int(math.sqrt(num))**2
circles = []
filled = 0
while True:
    for i in range(math.sqrt(cir_num)-1):
        for j in range(math.sqrt(cir_num)-1):
            c = Circle(frame, cir_num)
            circles.append(c)
    ##drawing
    for c in circles:
        if c.stage == 'empty':
            c.draw_empty()
        elif c.stage == 'filled':
            c.draw_filled()
    avar = 0
    for c in circles:
        avar += c.num2
    filled += avar

The definition of the power class is: like there is a number, and there is a function that takes 2 to the power of... the number.

The circle class is that there is a button. If the button stage is 'empty', I will draw the button with an empty circle. If a button with an empty circle is clicked, the button will have a filled circle that won't do anything.

This is the error message:

Traceback (most recent call last):
  File "C:/Users/roche/Documents/assignment.py", line 47, in <module>
    for i in range(math.sqrt(cir_num)-1):
TypeError: 'float' object cannot be interpreted as an integer

And the tkinter size is weird:

root.geometry('750x750+0+0')

gives me a vertical rectangle and I was expecting a square window. Thanks!

Upvotes: 1

Views: 1435

Answers (2)

BoarGules
BoarGules

Reputation: 16952

You are using range to mean much what it means in natural language, which is more or less a segment of the real number line, as in "temperature in the range 37.2C to 38.6C". But that is not what range() means in Python. In Python it means a bunch of contiguous integers. That is why you are getting complaints about the float you are calling it with.

Upvotes: 1

Rory Daulton
Rory Daulton

Reputation: 22544

You twice use the expression

range(math.sqrt(cir_num)-1)

The result of math.sqrt is a float value, so you are trying to get the range of a float value. As the documentation for range states,

The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method).

So if you really want to use range and math.sqrt together, you must convert the float to an int. This is probably what you want:

range(int(math.sqrt(cir_num))-1)

As one of the comments implies, range demanding an integer makes sense. If you ask for range(math.sqrt(5)), how is the program to loop from 0 to 2.2361? There is no obvious meaning for that, so Python requires to loop over only integer values. The numpy module does have the arange function and the linspace function, which can handle non-integer values, but the meaning of the ranges has been somewhat changed from Python's range.

Upvotes: 1

Related Questions