这是我的代码,这是一款棋盘游戏。我不知道为什么我会收到这个错误:
TypeError:“NoneType”类型的参数不可迭代。
问题出现在move_tiger()
函数中,但我在move_goat()
函数中使用了相同的代码,它运行正常。有人可以告诉我这个错误在我的情况下意味着什么,因为我不认为我试图迭代move_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
答案 0 :(得分:2)
在Python中,所有函数(和方法)都隐式返回None
,除非它们在执行期间到达显式return
语句。您的neighbours
方法似乎收到position
任何if-elif
分支未涵盖的else
。
尝试在neighbours
中添加position
分支,以便您可以看到意外的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)
:
public enum Language {
English=1,
Telugu=2,
Hindi=3,
Spanish=4
}
答案 1 :(得分:0)
当("b", 4)
值为self.neighbors(old)
时会发生异常,因为None
会返回neighbors
。
我不确定是否完全理解您的代码的作用,但也许您应该检查一下 <?php
if (isset($_POST['send'])) {
require_once JPATH_BASE . '/includes/config.php';
$firstname = $_POST['firstname'];
$fathername = $_POST['fathername'];
$grandfathername = $_POST['grandfathername'];
$familyname = $_POST['familyname'];
$sql = "INSERT INTO subventions (firstname,fathername,grandfathername,familyname) VALUES
('$firstname','$fathername','$grandfathername','$familyname')";
$inser = mysqli_query($con,$sql);
echo '<script type="text/javascript"> alert("sent"); </script>';
mysqli_close($inser);
}
?>
方法是否错过了一个案例。