python - 剪刀石头布。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)
解决方案
推荐阅读
- mysql - MYSQL - 执行过程以从表中执行语句
- prolog - 为什么我从 Prolog 中的递归查询中得到这个结果
- ios - 如何使用 Swift 使用 UIPickerView 选择 ip?
- c# -
在 web.config 中添加最后一个 lash 到 URL - botframework - Bot Framework + Facebook Tester = 无消息
- javascript - 创建两个日期之间的日期数组
- kubernetes - Storm Kubernetes livenessProbe
- sql - Oracle 错误,在预期的地方找不到 FROM 关键字
- node.js - 将 Heroku (Node.js) 服务器迁移到 Amazon S3
- ios - 如何在编辑时更改 uitextfield leftView 图像 tintColor