Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Missing one required positional argument

I am working on a game as a side project for fun and I have run into this error and I really don't know why it is happening...

Here is the code:

class players:
    def __init__(self, location, image_file, direction):
        self.location = location
        self.image_file = image_file
        self.direction = direction
        self.rect = self.image_file.get_rect()

    def turn(self, direction, playerImages):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] == True:
            self.direction -= 1
            if self.direction < -3:
                self.direction = 3
        if keys[pygame.K_d] == True:
            self.direction = 1
            if self.direction > 3:
                self.direction = 3

        if self.direction == -3:
            self.image_file = playerImages[0]
        if self.direction == -2:
            self.image_file = playerImages[1]
        if self.direction == -1:
            self.image_file = playerImages[2]
        if self.direction == 0:
            self.image_file = playerImages[3]
        if self.direction == 1:
            self.image_file = playerImages[4]
        if self.direction == 2:
            self.image_file = playerImages[5]
        if self.direction == 3:
            self.image_file = playerImages[6]

        return self.direction, self.image_file

I call it like:

skierDirection, playerImage = players.turn(skierDirection, playerImages)

The error I get is:

Traceback (most recent call last):
  File "C:\Users\Owen\Desktop\coding compile file\SkiFreeX\SkiFreeX.py", line 129, in <module>
    main()
  File "C:\Users\Owen\Desktop\coding compile file\SkiFreeX\SkiFreeX.py", line 122, in main
    skierDirection, playerImage = players.turn(skierDirection, playerImages)
TypeError: turn() missing 1 required positional argument: 'playerImages'
[Finished in 0.385s]

Any ideas?

like image 610
Owen Martin Avatar asked May 29 '18 23:05

Owen Martin


People also ask

How do you fix a missing one required positional argument?

The Python "TypeError: __init__() missing 1 required positional argument" occurs when we forget to provide a required argument when instantiating a class. To solve the error, specify the argument when instantiating the class or set a default value for the argument.

How do you fix missing positional arguments in python?

The Python "TypeError: missing 2 required positional arguments" occurs when we forget to provide 2 required arguments when calling a function or method. To solve the error, specify the arguments when calling the function or set default values for the arguments.

What are required positional arguments in python?

Positional ArgumentsThe first positional argument always needs to be listed first when the function is called. The second positional argument needs to be listed second and the third positional argument listed third, etc. An example of positional arguments can be seen in Python's complex() function.

How do you fix Syntaxerror positional argument follows keyword argument?

Using Positional Arguments Followed by Keyword Arguments One method is to simply do what the error states and specify all positional arguments before our keyword arguments! Here, we use the positional argument 1 followed by the keyword argument num2=2 which fixes the error.


2 Answers

You are not supposed to call a class method directly, instead create an instance of that class:

p1 = players(your, values, here)
skierDirection, playerImage = p1.turn(skierDirection, playerImages)

To elaborate on the error you're getting:

TypeError: turn() missing 1 required positional argument: 'playerImages'

It's because turn needs an instance of players as first argument (self). A class method always gets passed the instance as the first argument, thus p1.turn(skierDirection, playerImages) will acutually pass 3 parameters to players.turn.

like image 159
Mike Scotty Avatar answered Sep 29 '22 22:09

Mike Scotty


You have need to use () with class name

Follow below code

skierDirection, playerImage = players().turn(skierDirection, playerImages)
like image 22
sandeep shewale Avatar answered Sep 29 '22 22:09

sandeep shewale