TypeError: integer argument expected got float' Python 3.4

john taylor picture john taylor · Jul 25, 2015 · Viewed 7.2k times · Source

I get a error of a integer expected a float so I change it and then get the reverse saying a float can not be a integer. I had similar issues before and just changed it to have the int.

I am learning via tutorials in python27 but have for the most part got them to work in python34 via troubleshooting. This one I got stuck on.

TypeError: integer argument expected go float'

for tile in return_tiles:
    pygame.draw.circle(screen, [34, 95, 200],
    [tile.x + half - 2, tile.y + half - 2], 5 )

So I then made the change below it would usually work like the one under this one but in this case get a error

TypeError: 'float' object cannot be interpreted as a integer

   for tile in return_tiles:
        pygame.draw.circle(screen, (34, 95, 200), (int(tile.x + half -2),int(half - 2, tile.y + half - 2)), int(5))

example of how I fixed another one below that was a integer but in the case above it did not work

 pygame.draw.circle(screen, (34, 95, 200), (int(tile.x + half -2),int(half - 2, tile.y + half - 2)), int(5))

I been teaching myself by learning tutorials online but most are for earlier version 2.7 etc of python. But that has not been a problem for the most part. I been just using the the error msg and for the most part I can figure it out if not sometimes I run 2to3.py or search to find the answer.

Answer

bakkal picture bakkal · Jul 25, 2015

I suspect your culprit is the half variable, since it involves a division, in Python 2 it'll give you an int, however in Python 3 a division will give a float unless you use the int division operator //

If you're using Python 2 tutorials, they didn't have to deal with that

Python 3

>>> 5 // 3
1
>>> 5 / 3
1.6666666666666667

So I would go look for where half is calculated, and either coerce to int with half = int(...) or use //

In pygame it's not very useful to use float for pixel dimensions, so just keep everything in int, especially when you do divisions like for half, the rest of arithmetics like tile.x + half will yield an int if both operands are ints, so there is no need to coerce