python - 从单独的 Python 文件调用方法时出错
问题描述
我正在尝试用 Python 制作井字游戏,我在其中使用两个单独的文件。每个都有不同的代码和单独的类,我试图调用它们。但是,每次我尝试这样做时,都会收到此错误:
def __init__(self, letter): super().__init__(letter)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
为什么会这样?我已经导入了正确的文件(如下)并使用代码(下面)来调用它。
from player import NormalPlayer, ComputerPlayer
--
if __name__ == "__main__":
x_player = NormalPlayer("X")
o_player = ComputerPlayer("O")
ttt = TicTacToe()
play(ttt,
x_player,
o_player,
print_game = True)
我没有包含它,但是如果您需要我提供两个文件中的所有代码,请告诉我。但是,如果您从这段代码中知道我的错误是什么,请告诉我。谢谢!
# 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
解决方案
您没有指定从哪个类继承。要使用继承,您可以使用class ClassName(ClassToInheritFrom)
.
所以你的文件看起来像:
# 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(Player):
# 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(Player):
# 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
推荐阅读
- omnet++ - 为什么 INET Aodv 路由在 OMNeT++ 中不起作用?
- sharepoint - 为什么我不能将 SharepointForm1 重命名为其他名称?
- keystore - 我应该为每个要加密的字符串创建一个新的 SecretKey 吗?
- python - 从 html 列表返回 json 字符串
- python - 类数,4,与 target_names 的大小,6 不匹配。尝试指定标签参数
- sql - 在 SQL Plus 中更新查询而不是提交并指导我在另一个会话中提交
- ios - 如何按字母顺序对 API 中的 JSON 数组进行排序?
- python - 我在 github 中复制了一个 tkinter 库代码,现在我遇到了缩进错误
- reactjs - 使用 reactjs 在生产模式下加载图像的问题
- html - 固定元素受到溢出的影响:隐藏的父级