[Tutor] Type error when doing Pygame

Cameron Simpson cs at cskk.id.au
Sat Sep 28 06:47:52 EDT 2019


On 28Sep2019 22:20, DL Neil <PyTutor at DancesWithMice.info> wrote:
>On 28/09/19 7:48 PM, Cravan wrote:
>>                 Recently tried doing a pygame project (which is still 
>> half-finished). However I received a TypeError code that 5 positional 
>> arguments have been inserted when I have only inserted four in my 
>> function. Have no idea why its not working.
>...
>
>>And here’s the Error I’m receiving:
>>Traceback (most recent call last):
>>   File "final_project.py", line 135, in <module>
>>     g.run()
>>   File "final_project.py", line 37, in run
>>     self.draw()
>>   File "final_project.py", line 65, in draw
>>     self.food_food(screen, 5, 5, self.food_bar)
>>TypeError: food_food() takes 4 positional arguments but 5 were given
>
>It's always worth looking carefully at the information contained within 
>the Traceback. In this case, we're being told that the method is being 
>given more arguments than it needs.
>
>Should the first/"screen" argument be included?
>(it does not appear to be used within the method)

Additionally, this eror is often confusing.

When you call some method:

  obj.method(a,b)

it calls ObjClass.method(self,a,b).

So you've overtly called it with 2 arguments, but the method is invokes 
with 3 arguments because "self" is automatically supplied.

So in your error message you called:

  self.food_food(screen, 5, 5, self.food_bar)

which has 4 arguments, but "food_food" gets those arguments with "self" 
at the front, so the complaint is that "5 were given".

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list