从单独的Python文件调用方法时出错
我正在尝试用Python制作一个Tic-Tac-Toe游戏,其中我使用两个单独的文件。每个都有不同的代码和独立的类,我正试图调用它们。但是,每次尝试这样做时,我都会收到以下错误:从单独的Python文件调用方法时出错,python,file,class,import,Python,File,Class,Import,我正在尝试用Python制作一个Tic-Tac-Toe游戏,其中我使用两个单独的文件。每个都有不同的代码和独立的类,我正试图调用它们。但是,每次尝试这样做时,我都会收到以下错误: def __init__(self, letter): super().__init__(letter) TypeError: object.__init__() takes exactly one argument (the instance to initialize) 为什么会发生这种情况?我已经导入了正确的文
def __init__(self, letter): super().__init__(letter)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
为什么会发生这种情况?我已经导入了正确的文件(如下),并使用代码(在下面)调用它
from player import NormalPlayer, ComputerPlayer
--
我还没有包括它,但如果您需要我提供这两个文件中的所有代码,请让我知道。如果你知道我的错误是从这个代码,但是,请让我知道。谢谢大家!
# Recognise the player as an object, with a specific letter (X or O)
class Player:
# Set the player's letter (noughts or crosses)
def __init__(self, letter): self.letter = letter
# Turn-based system
def get_move(self, game): pass
# Use the inheritance of classes to create a computer player that uses the 'Player' class
class ComputerPlayer:
# Set the computer's letter with teh super class
def __init__(self, letter): super().__init__(letter)
# Turn-based system, get a random move from all open possibilities
def get_move(self, game):
choice = random.choice(game.open_moves())
return choice
# Use the inheritance of classes for the user object that uses the 'Player' class
class NormalPlayer:
# Set the user's letter with the super class
def __init__(self, letter): super().__init__(letter)
# Turn-based system, get the player's movement choice
def get_move(self, game):
# Player's choice must first be verified
verified = False
# The spot value of the move the player wants to make
value = None
# Ask for the player's move
while not verified:
choice = input(self.letter + "\s turn. Which spot do you want to play? ")
# Check if the player's choice is valid...
try:
# Turn the input into an integer
value = int(choice)
# If the spot value is not available, catch the error and tell the player
if value not in game.open_moves():
raise ValueError
# ...if it is, then announce it
except ValueError:
# If the choice was invalid, have the player decide their move again
print("The spot value is not valid. Please choose another spot.")
return value
您没有指定从哪个类继承。要使用继承,请执行
类ClassName(ClassToInheritFrom)
因此,您的文件如下所示:
#将玩家识别为具有特定字母(X或O)的对象
职业球员:
#设置玩家的字母(零或十字)
定义初始字母(self,字母):self.letter=字母
#回合制
def get_移动(自我,游戏):传球
#使用类的继承来创建使用“player”类的计算机播放器
类计算机播放器(播放器):
#用超级类设置计算机的字母
def(self,字母):super()。(字母)
#基于回合的系统,从所有打开的可能性中获得随机移动
def get_移动(自我,游戏):
choice=random.choice(game.open_moves())
返回选择
#对使用“Player”类的用户对象使用类的继承
职业球员(球员):
#使用超类设置用户的字母
def(self,字母):super()。(字母)
#基于回合的系统,获得玩家的移动选择
def get_移动(自我,游戏):
#玩家的选择必须首先得到验证
已验证=错误
#玩家想要做出的移动的现货价值
值=无
#请求玩家移动
虽然未经核实:
选择=输入(self.letter+“\s回合。您想玩哪个位置?”)
#检查玩家的选择是否有效。。。
尝试:
#将输入转换为整数
value=int(选项)
#如果现场值不可用,捕捉错误并告诉玩家
如果值不在游戏中。打开\u移动():
升值误差
#…如果是,那么就宣布
除值错误外:
#如果选择无效,让玩家再次决定他们的移动
打印(“点值无效。请选择其他点。”)
返回值
此外,似乎有些人对这篇文章投了反对票。我不确定这是为什么,因为我已经提供了必要的信息,并收到了回复。如果一个人能理解我的问题并回答它,为什么会被否决?它很清楚,并提供了我正在使用的代码。如果你投了反对票,请让我知道我能做些什么来让这更清楚。
# Recognise the player as an object, with a specific letter (X or O)
class Player:
# Set the player's letter (noughts or crosses)
def __init__(self, letter): self.letter = letter
# Turn-based system
def get_move(self, game): pass
# Use the inheritance of classes to create a computer player that uses the 'Player' class
class ComputerPlayer:
# Set the computer's letter with teh super class
def __init__(self, letter): super().__init__(letter)
# Turn-based system, get a random move from all open possibilities
def get_move(self, game):
choice = random.choice(game.open_moves())
return choice
# Use the inheritance of classes for the user object that uses the 'Player' class
class NormalPlayer:
# Set the user's letter with the super class
def __init__(self, letter): super().__init__(letter)
# Turn-based system, get the player's movement choice
def get_move(self, game):
# Player's choice must first be verified
verified = False
# The spot value of the move the player wants to make
value = None
# Ask for the player's move
while not verified:
choice = input(self.letter + "\s turn. Which spot do you want to play? ")
# Check if the player's choice is valid...
try:
# Turn the input into an integer
value = int(choice)
# If the spot value is not available, catch the error and tell the player
if value not in game.open_moves():
raise ValueError
# ...if it is, then announce it
except ValueError:
# If the choice was invalid, have the player decide their move again
print("The spot value is not valid. Please choose another spot.")
return value