I keep getting the error: TypeError: integer argument expected, got float in Python 3.6.5
I keep getting the error: TypeError: integer argument expected, got float in Python 3.6.5
I am using Python version 3.6.5, downloaded from Python.org. While programming, I keep getting the error TypeError: integer argument expected, got float
. I am using Python version 3.6.5, win 32 bit. Here is my complete code yet (to be worked on still):
TypeError: integer argument expected, got float
import pygame
pygame.init()
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
sky_blue = (0,150,225)
green = (0,255,0)
displayWidth = 800
displayHeight = 600
#Final :- pygame.display.set_mode((1365, 1050))
gameDisplay = pygame.display.set_mode((displayWidth,displayHeight))
pygame.display.set_caption('Super Mario')
clock = pygame.time.Clock()
crashed = False
timeOut = False
Quit = False
#50,75
marioStanding = pygame.image.load('Super_Mario_Standing.jpg')
marioStanding = pygame.transform.scale(marioStanding, (displayWidth/40,displayHeight/8))
def Stand(x,y):
gameDisplay.blit(marioStanding,(x,y))
x = (displayWidth * 0.45)
y = (displayHeight * 0.8)
while not crashed and not timeOut and not Quit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Quit = True
print (event)
gameDisplay.fill(sky_blue)
Stand(x,y)
pygame.display.update()
clock.tick(24)
pygame.quit()
quit()
I was not getting any error before removing the height and width as 50,75
, and replacing it with displayWidth/40, displayHeight/8
, so I'm guessing that it has something to do with that. Here is the full error:
50,75
displayWidth/40, displayHeight/8
Traceback (most recent call last):
File "C:UsersDellDesktopSuper_Mario_Python3.6Billionth_Pygame_Test.py", line 28, in <module>
marioStanding = pygame.transform.scale(marioStanding, (displayWidth/40,displayHeight/8))
TypeError: integer argument expected, got float
1 Answer
1
Welcome to python 3 floating point division. Here:
pygame.transform.scale(marioStanding, (displayWidth/40,displayHeight/8))
your divisions, even if made between integers, create a tuple of float elements.
>>> 200/40
5.0
Use integer division to keep them as integers (this wasn't the case in python 2) so the scale
function accepts them as arguments.
scale
>>> 200//40
5
like this:
pygame.transform.scale(marioStanding, (displayWidth//40,displayHeight//8))
Note: it also works in python 2.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Welcome to Stack Overflow. Your example could be reduced to about 4 lines of code. This makes the question clearer and improves the likelihood of good answer. See how to create a Minimal, Complete, and Verifiable example.
– Peter Wood
Aug 28 at 21:27