Traceback (most recent call last):IndexError: list assignment index out of range
今天在写n-皇后python代码时,遇到了这个报错:Traceback (most recent call last):IndexError: list assignment index out of range。
先上结论,trackback其实是一个异常追踪栈,所以应该看最后一个抛出异常的位置,也就是报错信息最后一行。在我这里定位到是 g[x][y]='.'列表越界
代码如下:
N=100
n=int(input())
g=[[0]*n for _ in range(n)]
row,col,dg,udg=[0]*N,[0]*N,[0]*2*N,[0]*2*Ndef dfs(x,y,s):
#处理每行遍历完之后的越界问题if(y>n):y=0x=x+1 if(x==n):if(s==n):for i in range(n):print("".join(g[i]))print("")return
#不放g[x][y]='.'dfs(x,y+1,s)
#放if (not row[x]) and (not col[y]) and (not dg[x+y]) and (not ugd[y-x+n]):g[x][y]='Q'row[x]=1col[y]=1dg[x+y]=1udg[y-x+n]=1dfs(x,y+1,s+1)g[x][y]='.'row[x]=0col[y]=0dg[x+y]=0udg[y-x+n]=0
dfs(0,0,0)
报错如图:
Traceback异常追踪栈, 第一层try catch,如果异常没有被处理,则会跑出去给函数调用者。如果下一层还是没被处理,则继续抛出去。如果抛到最后一层都还是没被处理,那就只能终止程序了,并且打印异常信息,即上述截图的打印过程。
具体的找错误的过程,我们可以通过自己打印异常信息来帮助寻找。既然已经找到了抛出异常的位置,我们可以接住异常,看看是怎么个越界发法。
#不放try:g[x][y]='.'except IndexError as e:raise ValueError("异常信息{},x:{},y:{}".format(str(e),x,y))
x=0,y=4的时候发生了越界。定位到了原因,我这里下标是从0开始的,按理说y=n-1的时候就应该结束,所以错误代码在这里:
if(y>n):y=0x=x+1
应该改为if(y>=n):y=0x=x+1
Traceback (most recent call last):IndexError: list assignment index out of range相关推荐
- 【python基础】list操作相关:扩充、条件查找【IndexError: list assignment index out of range】...
参考 [https://blog.csdn.net/qq_44768814/article/details/88614393] [IndexError: list assignment index o ...
- python报错: arry[i] = t IndexError: list assignment index out of range
此处报错的原因是arry是一个空列表,空列表不能按照下标索引的方法赋值 解决方法:使用方法.append() def binself(x):arry=[]i=0while(x):t=x%2x=x//2 ...
- python list越界原因(list assignment index out of range)
分析: list assignment index out of range:列表超过限制 情况1:list[index]index超出范围 情况2:list是一个空的,没有一个元素,进行list[0 ...
- 富文本编辑器图片上传失败的BUG解决:IndexError:list index out of range
富文本编辑器图片上传失败的BUG解决 问题原因 我们将通过Django上传的图片保存到了FastDFS中,而保存在FastDFS中的文件名没有后缀名(.png/.jpg/.jif),而ckeditor ...
- IndexError: list index out of range 错误原理及解决方法(python)
list index out of range 解决思路 发现问题 当我在写文章金融数据分析(一)python预热中的项目二:计算手机销量年增长率的时候,出现过如下报错: 看着结果是已经跑出来了,但是 ...
- Traceback (most recent call last): File AttributeError: 'NoneType' object has no attribute 'group'
Traceback (most recent call last):File "<stdin>", line 1, in <module> Attribut ...
- Traceback (most recent call last)AttributeError: ‘NumpyArrayIterator‘ object has no attribute ‘ndim‘
成功解决该问题! Traceback (most recent call last): File "C:/Users/-/Desktop/pythonProject2/main.py&quo ...
- python traceback most recent_pyspider启动错误: Traceback (most recent call last):
安装了基于pyhton的爬虫pyspider. 用命令sudo pyspider all启动pyspider的时候报错: Traceback (most recent call last): File ...
- pip 10.0.0 BUG 解决方案Traceback (most recent call last): File /usr/bin/pip3, line 9, in module...
使用pip3 出现以下错误: Traceback (most recent call last): File "/usr/bin/pip3", line 9, in from pi ...
- python安装包的时候报错 ERROR: Exception: Traceback (most recent call last): File “C:\Users\
Python下载scrapy的时候报错了,如下图: ERROR: Exception: Traceback (most recent call last): File "C:\Users\西 ...
最新文章
- C语言从0到1·变量
- linux ntfs 用户权限,Linux在NTFS中创建的文件的权限
- audacity_如何在Audacity中快速编辑多个文件
- LeetCode547. Friends Circles 利用union find | bfs | dfs三种方法解决
- Oracle 1204 RAC failover 测试 (一)
- 16进制在c语言中用来做什么,十六进制在代码中有什么用
- MySQL中实现连续日期内数据统计,缺省天数0补全
- 操作系统进程间通信简述
- 第11章 支撑向量机 SVM 学习笔记 下 SVM思路解决回归问题
- 台式计算机设备验收单,电脑设备验收单.doc
- ADAPT-PT v8.0(房屋与停车场结构里的梁、板、楼板系统之先进后拉预力软件)
- Linux下C语言开发通讯录管理软件(一)
- 洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集
- 奇异矩阵和非奇异矩阵 行列式矩阵简单理解 代数意义 几何意义 行列式的定义: 二阶行列式的几何意义: 三阶行列式的几何意义: 行列式化为对角形的几何解释: 二阶行列式乘积项的几何意
- 康宁发布第五代大猩猩玻璃 坚韧度更强更耐摔
- Hopping Rabbit---牛客
- AD19 DRC 时弹出 Design contains shelved or modified (but not repoured) polygons
- VM-tools选项为灰色无法安装的问题
- 英勇青铜5基金个人策略
- 云享团——基于大数据开发套件的增量同步策略