Python ';列表';对象没有属性';董事会';
编辑:希望这能给你更多的信息 当我尝试运行这个时,我得到了错误Python ';列表';对象没有属性';董事会';,python,python-3.x,Python,Python 3.x,编辑:希望这能给你更多的信息 当我尝试运行这个时,我得到了错误 class tictactoe: def __init__(self): # created the self.board # self.board list 3x3 self.board = [['', '', ''], ['', '', ''], ['', '', '']] def empt
class tictactoe:
def __init__(self): # created the self.board
# self.board list 3x3
self.board = [['', '', ''],
['', '', ''],
['', '', '']]
def empty_places(self): # empty places in board
empty = []
for i in range(3):
for j in range(3):
if self.board[i][j] == '': #LINE WITH THE PROBLEM
empty.append([i, j])
return empty
def game(self):
isTie = ai.is_tie(self.board)
print(isTie)
class ai:
def is_tie(board):
isTie = False
if len(tictactoe.empty_places(board)) == 0:
isTie = True
return isTie
myGame = tictactoe()
myGame.game()
在def空位的第四行中
我做错了什么
'list' object has no attribute 'board'
输出:
['done'、'done'、'done'、'done'、'done'、'done'、'done'、'done']我尝试运行您的代码,但没有出现任何错误,它似乎工作正常:
class tictactoe:
def __init__(self, ): # created the self.board
# self.board list 3x3
self.board = [['', '', ''],
['', '', ''],
['', '', '']]
def empty_places(self): # empty places in board
empty = []
for i in range(3):
for j in range(3):
if self.board[i][j] == '': #LINE WITH THE PROBLEM
empty.append("done")
return empty
tic = tictactoe()
print(tic.empty_places())
如果我运行上面的代码。以下是印刷品
##### your code
class tictactoe:
def __init__(self): # created the self.board
# self.board list 3x3
self.board = [['', '', ''],
['', '', ''],
['', '', '']]
def empty_places(self): # empty places in board
empty = []
for i in range(3):
for j in range(3):
if self.board[i][j] == '': #LINE WITH THE PROBLEM
empty.append([i, j])
return empty
#### What I added
# create a board object
board = tictactoe()
# check for empty places
empty_places_list = board.empty_places()
# print the list with empty spaces
print(empty_places_list)
我试着用Python 3.7在空闲和Spyder上运行它。也许这已经对您有所帮助,否则我建议提供更多上下文或关于如何/在何处运行代码的更多信息。问题出在def is tie上的类ai上 如果len(tictactoe.empty_places(board))==0,而不是
,则尝试检查列表是否为空
如果tictactoe.empty\u places==[]:
谢谢大家的帮助。不清楚
ai
课程的目的是什么。它唯一的实例方法是将板
列表作为参数。这已经不像实例方法那样常规了,而且该参数实际上应该属于该类
然后您将板上的列表传递回空位置
,该位置再次期望获得tictactoe
对象
克服这一问题的一种方法是使用ai
类,使is\u tie
a接受tictactoe
对象:
ai类:
@类方法
def是tie(cls,游戏):
return len(game.empty_places())==0
然后在game
方法中将其称为isTie=ai.is_tie(self)
但是整个ai
类变得有点不相关,看起来is_tie
方法可以改为tictactoe
方法:
tictactoe类:
def_uuinit_uu(self):#创建self.board
#自助板列表3x3
self.board=[['','',''],
['', '', ''],
['', '', '']]
def空位(自身):#板中空位
空=[]
对于范围(3)中的i:
对于范围(3)内的j:
如果self.board[i][j]='':
empty.append([i,j])
返回空
def是_tie(自身):
返回len(self.empty_places())==0
def游戏(自我):
isTie=self.is_tie()
打印(isTie)
您共享的代码没有问题。您共享了错误的代码/错误来自另一个代码这实际上不是在类
定义中吗?@Gal-共享python中的每一行代码module@balderman这不是一个好建议。我们不需要全部代码,只需要一部分it@Tomerikoo因为OP不能指出问题所在,我不确定他是否能够创建最小的示例..不,那没有意义tictactoe.empty\u places
是一种方法(callable
object)。所以if语句总是False
。即使如此,您也不会遇到异常
程序的行为也不会像您预期的那样。(逻辑错误)我再检查一遍。虽然我很确定这是可行的。这是不正确的。空位置只是一个函数对象,而不是调用。因此,它总是与空列表不同。详细解释请参见我的答案。我认为您应该删除此答案并阅读
[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]