Python &引用;TypeError:参数类型为';非类型';是不是很难理解;?

Python &引用;TypeError:参数类型为';非类型';是不是很难理解;?,python,Python,这是我的代码,这是一个棋盘游戏。我不知道为什么会出现这样的错误: TypeError:类型为“NoneType”的参数不可编辑 问题出现在move\u tiger()函数中,但我在move\u tiger()函数中使用了相同的代码,它工作正常。有人能告诉我这个错误在我的例子中是什么意思吗,因为我认为我没有试图迭代move\u tiger()函数中的任何内容。谢谢你的帮助 class Board: def __init__(self): self.board = dic

这是我的代码,这是一个棋盘游戏。我不知道为什么会出现这样的错误:

TypeError:类型为“NoneType”的参数不可编辑

问题出现在
move\u tiger()
函数中,但我在
move\u tiger()
函数中使用了相同的代码,它工作正常。有人能告诉我这个错误在我的例子中是什么意思吗,因为我认为我没有试图迭代
move\u tiger()
函数中的任何内容。谢谢你的帮助

class Board:

    def __init__(self): 
        self.board = dict()
        # a, b, c, d, and e are keys
        self.board['a'] = ['T']
        self.board['b'] = ['0','T','0','0','T','0']
        self.board['c'] = ['0','0','0','0','0','0']
        self.board['d'] = ['0','0','0','0','0','0']
        self.board['e'] = ['0','0','0','0']
        self.phase = 'add'
        self.numgoats = 0

    def print_board(self):
        for letter in 'abcde':
            for vertex in self.board[letter]:
                print vertex,
            print

    def content(self, position):
        row=list(position)[0]
        column=list(position)[1]
        return self.board[row][int(column)]

    def _set(self, position, value):
        self.board[position[0]][position[1]] = value

    def neighbors(self, position):
        if position == ('a',0):
            return [('b',1),('b',2),('b',3),('b',4)]
        # bunch of extraneous elif's that all return something skipped
        elif position == ('e',3):
            return [('e',2),('d',4)]

    def add_goat(self, position):
        if self.phase == 'add':
            if self.content(position) == '0':
                self._set(position, 'G')
                self.numgoats+=1
                if self.numgoats==3:
                    self.phase = 'move'

    def move_goat(self, old, new):
        if self.phase!='move':
            print "invalid move; try again"
        elif self.content(old) == 'G' and self.content(new) == '0' and new in self.neighbors(old):
            self.board[old[0]][old[1]] = '0'
            self.board[new[0]][new[1]] = 'G'
        else:
            print "invalid move; try again"

    def move_tiger(self, old, new):
        if self.content(old) == 'T' and self.content(new) == '0' and new in self.neighbors(old):
                self.board[old[0]][old[1]] = '0'
                self.board[new[0]][new[1]] = 'T'
        else:
            print "invalid move; try again"


myboard = Board()
myboard.print_board()
myboard.add_goat(('b',0))
myboard.print_board()
myboard.add_goat(('c',0))
myboard.print_board()
myboard.add_goat(('d',0))
myboard.print_board()
myboard.move_goat(('c',0),('c',1))
myboard.print_board()
myboard.move_goat(('c',1),('b',5))
myboard.print_board()
myboard.move_tiger(('b',4),('b',3))
myboard.print_board
此代码生成以下内容:

T
0 T 0 0 T 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0
T
G T 0 0 T 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0
T
G T 0 0 T 0
G 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0
T
G T 0 0 T 0
G 0 0 0 0 0
G 0 0 0 0 0
0 0 0 0
T
G T 0 0 T 0
0 G 0 0 0 0
G 0 0 0 0 0
0 0 0 0
invalid move; try again
T
G T 0 0 T 0
0 G 0 0 0 0
G 0 0 0 0 0
0 0 0 0
Traceback (most recent call last):
  File "game2.py", line 110, in <module>
    myboard.move_tiger(('b',4),('b',3))
  File "game2.py", line 91, in move_tiger
    if self.content(old) == 'T' and self.content(new) == '0' and new in self.neighbors(old):
TypeError: argument of type 'NoneType' is not iterable
T
0 T 0 T 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0
T
G T 0 T 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0
T
G T 0 T 0
G000
0 0 0 0 0 0
0 0 0 0
T
G T 0 T 0
G000
G000
0 0 0 0
T
G T 0 T 0
0 G 0 0 0 0 0
G000
0 0 0 0
无效动作;再试一次
T
G T 0 T 0
0 G 0 0 0 0 0
G000
0 0 0 0
回溯(最近一次呼叫最后一次):
文件“game2.py”,第110行,在
我的棋盘。移动老虎(('b',4),('b',3))
文件“game2.py”,第91行,在move_tiger中
如果self.content(旧版)='T'和self.content(新版)='0',self.neights(旧版)中的self.content(新版):
TypeError:类型为“NoneType”的参数不可编辑
在Python中,所有函数(和方法)都隐式返回
None
,除非它们在执行期间到达显式的
return
语句。您的
邻居
方法似乎接收到一个
位置
,该位置未被任何
if elif
分支覆盖

尝试在
邻居中添加
else
分支,以便可以看到意外的
位置

def neighbors(self, position):
    if position == ('a',0):
        return [('b',1),('b',2),('b',3),('b',4)]
    elif position[0] == ('b',0):
        return [('b',1),('c',0),('c',1)]
    # ...
    else:
        raise ValueError(position)
在Python中,所有函数(和方法)都隐式返回
None
,除非它们在执行期间到达显式
return
语句。您的
邻居
方法似乎接收到一个
位置
,该位置未被任何
if elif
分支覆盖

尝试在
邻居中添加
else
分支,以便可以看到意外的
位置

def neighbors(self, position):
    if position == ('a',0):
        return [('b',1),('b',2),('b',3),('b',4)]
    elif position[0] == ('b',0):
        return [('b',1),('c',0),('c',1)]
    # ...
    else:
        raise ValueError(position)

old
值为
(“b”,4)
时会发生异常,因为
self.neighbories(old)
返回
None

我不确定是否完全理解代码的作用,但也许您应该检查一下在
邻居
方法中是否遗漏了一个案例。

old
值为
(“b”,4)
时会发生异常,因为
self.neights(old)
返回
None

我不确定是否完全理解你的代码,但也许你应该检查一下你是否在
邻居方法中遗漏了一个案例。

请只包含相关代码-人们不会费心阅读所有代码,因此你不太可能得到答案。
self.neights(旧)
返回
。请只包含相关代码-人们不会费心通读所有代码,因此你不太可能得到答案。
self.neights(old)
返回