Home > Software engineering >  When calling an OOP function, TypeError: takes 1 positional argument but 2 were given. How do I fix
When calling an OOP function, TypeError: takes 1 positional argument but 2 were given. How do I fix

Time:05-26

I am trying to make a wordle game in python. However, when I run the following code, I get a TypeError: print_guess() takes 1 positional argument but 2 were given.

from colorama import init, Fore, Back, Style

class game:
    def __init__(self, guess, answer):
    self.guess = guess
    self.answer = answer

    def get_eval(self): # shortened version of full get_eval() function. 
    output = [0,0,0,0,0]
    for letter in range(5):
        if self.answer[letter] == self.guess[letter]: # check if correct letter is in the correct place.
        output[letter] = 2
    return tuple(output)

    def print_guess(self):
    colors = ['BLACK', 'YELLOW', 'GREEN']
    for i in range(5):
        colour = getattr(Back, colors[self.get_eval(self)[i]]) # TypeError: get_eval() takes 1 positional argument but 2 were given
        print(Style.BRIGHT   Fore.WHITE   colour   self.guess[i].upper(), end=' ')

game_dis = game('guess', 'words')
game_dis.print_guess(game_dis.guess) # TypeError: print_guess() takes 1 positional argument but 2 were given`

CodePudding user response:

You have the "self" parameter as an argument of your method. This is basically the instance of your class object. This is a argument that you don't have to pass yourself but is being passed automatically. So when you give an additional argument as you do, you are giving two.

Change your code to this:

game_dis.print_guess() 
  • Related