列表被同时识别为bool 和 list类型
我正在写八皇后代码,尝试运行,出现错误提示Traceback (most recent call last):
File ".\eightqueen.py", line 63, in <module>
dfs(0)
File ".\eightqueen.py", line 44, in dfs
dfs(deep+1)
File ".\eightqueen.py", line 39, in dfs
if row == False and x_row == False and y_row == False :
TypeError: 'bool' object is not subscriptable
代码如下:
"""
Ver: 1.0
Date: 2020年8月16日08:59:02
Auther: CCandle
"""
####### definition of arrays ######
N = 8
map = []
for i in range(0, N+1):
line = []
for j in range(0, N+1):
line.append(False)
map.append(line)
# create a N*N map filles with False
# to store the queens' positions
row = []
x_row = []
y_row = []
for i in range(0, N+1):
row.append(False)
for i in range(0, N*2 + 1):
x_row.append(False)
y_row.append(False)
# to mark which row/x_row/y_row is occupied
###### definitions of functions ########
def dfs(deep):
global row
global x_row
global y_row
if(deep >= N):
print_map()
return
for i in range(0, N+1):
if row == False and \
x_row == False \
and y_row == False :
row = True
x_row = True
y_row = True
map = True
dfs(deep+1)
row = False
x_row = False
y_row = False
map = False
return
def print_map():
for i in range(0, N+1):
for j in range(0, N+1):
if map:
print(' ', end='*')
else:
print(' ', end=' ')
print()
########## call the function ###########
dfs(0)
百度后发现好像是给bool类型下标了,但是我检查代码发现并没有这一问题,后来我在编辑器中看到提示:
x_row: bool
x_row: list
我就蒙了,同一个变量怎么还出来两个类型,而且我上一行的row变量就是个单纯的列表
请问各位我是哪个地方出现了问题 """
Ver: 1.0
Date: 2020年8月16日08:59:02
Auther: CCandle
"""
####### definition of arrays ######
N = 8
map = []
for i in range(0, N+1):
line = []
for j in range(0, N+1):
line.append(False)
map.append(line)
# create a N*N map filles with False
# to store the queens' positions
row = []
x_row = []
y_row = []
for i in range(0, N+1):
row.append(False)
for i in range(0, N*2 + 1):
x_row.append(False)
y_row.append(False)
# to mark which row/x_row/y_row is occupied
###### definitions of functions ########
def dfs(deep):
global row
global x_row
global y_row
if(deep >= N):
print_map()
return
for i in range(0, N+1):
if row == False and \
x_row == False \
and y_row == False :
row = True
x_row = True
y_row = True
map = True
dfs(deep+1)
row = False
x_row = False
y_row = False
map = False
return
def print_map():
for i in range(0, N+1):
for j in range(0, N+1):
if map:
print(' ', end='*')
else:
print(' ', end=' ')
print()
########## call the function ###########
dfs(0) ba21 发表于 2020-8-16 13:38
噢噢噢噢!!!我傻了
页:
[1]