首页 > 解决方案 > 剪刀石头布。Udacity,类和子类

问题描述

我收到此错误:

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)

标签: pythonfunctionclassmodulesubclass

解决方案


推荐阅读