3

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.

4

1 回答 1

6

我怀疑你的罪魁祸首是half变量,因为它涉及一个除法,在 Python 2 中它会给你一个int,但是在 Python 3 中一个除法会给你一个,float除非你使用 int 除法运算符//

如果您使用的是 Python 2 教程,他们不必处理这个问题

蟒蛇 3

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

所以我会去寻找half计算的地方,或者强制int使用half = int(...)或使用//

float在 pygame 中,用于像素尺寸并不是很有用,所以只需将所有内容保留在 中int,尤其是当您进行像 for 之类的除法时,如果两个操作数都是s,half其余算术tile.x + half将产生一个,因此无需强制intint

于 2015-07-25T15:49:59.710 回答