我收到此错误:
move1 = self.p1.move()
> AttributeError: 'str' object has no attribute 'move'
基本上,我试图了解如何从一个类中调用模块。 Player或RandomPlayer类。来自游戏类。
以下是一些代码:
moves = ['rock', 'paper', 'scissors']
class Player():
def move(self):
return 'rock'
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
class RandomPlayer(Player):
def move(self):
p1 = random.choice(moves)
p2 = random.choice(moves)
return (p1, p2)
class Game():
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def play_round(self, p1, p2):
move1 = self.p1.move()
move2 = self.p2.move()
print(f"Player 1: {move1} Player 2: {move2}")
self.p1.learn(move1, move2)
self.p2.learn(move2, move1)
return (move1,move2)