When calling an OOP function, TypeError: takes 1 positional argument but 2 were given. How do I fix this?
When calling an OOP function, TypeError: takes 1 positional argument but 2 were given. How do I fix this?
我正在尝试在 python 中制作文字游戏。但是,当我 运行 以下代码时,我得到 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`
您将“self”参数作为方法的参数。这基本上是 class 对象的实例。这是一个您不必自己通过但自动通过的论点。所以当你像你一样给出一个额外的参数时,你给出了两个。
将您的代码更改为:
game_dis.print_guess()
我正在尝试在 python 中制作文字游戏。但是,当我 运行 以下代码时,我得到 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`
您将“self”参数作为方法的参数。这基本上是 class 对象的实例。这是一个您不必自己通过但自动通过的论点。所以当你像你一样给出一个额外的参数时,你给出了两个。
将您的代码更改为:
game_dis.print_guess()