python中的错误如何查看
作者:silencement 时间:2021-05-03 08:31:22
python常见的错误有
1.NameError变量名错误
2.IndentationError代码缩进错误
3.AttributeError对象属性错误
4.TypeError类型错误
5.IOError输入输出错误
6.KeyError字典键值错误
具体介绍
1.NameError变量名错误
报错:
>>> print a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
解决方案:
先要给a赋值。才能使用它。在实际编写代码过程中,报NameError错误时,查看该变量是否赋值,或者是否有大小写不一致错误,
或者说不小心将变量名写错了。
注:在Python中,无需显示变量声明语句,变量在第一次被赋值时自动声明。
推荐学习《python教程》。
>>> a=1
>>> print a
1
2.IndentationError代码缩进错误
代码
a=1b=2
if a<b:
print a
报错:
IndentationError: expected an indented block
原因:
缩进有误,python的缩进非常严格,行首多个空格,少个空格都会报错。这是新手常犯的一个错误,由于不熟悉python编码规则。像def,class,if,for,while等代码块都需要缩进。
缩进为四个空格宽度,需要说明一点,不同的文本编辑器中制表符(tab键)代表的空格宽度不一,如果代码需要跨平台或跨编辑器读写,建议不要使用制表符。
解决方案
a=1b=2
if a<b:
print a
3.AttributeError对象属性错误
报错:
>>> import sys
>>> sys.Path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Path'
原因:
sys模块没有Path属性。
python对大小写敏感,Path和path代表不同的变量。将Path改为path即可。
>>> sys.path
['', '/usr/lib/python2.6/site-packages']
内容扩展:
python 查看错误类型
‘''
查看错误类型
‘''
try:
a = int(input(‘请输入被除数'))
b = int(input(‘请输入除数'))
print(a/b)
print('******************')
except Exception as m:
print(m)
来源:https://www.py.cn/faq/python/15448.html
标签:python,错误查看
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python离线安装openpyxl模块的步骤
2021-08-10 16:04:04
![](https://img.aspxhome.com/file/2023/6/89286_0s.png)
Bootstrap Metronic完全响应式管理模板之菜单栏学习笔记
2023-08-17 10:58:20
![](https://img.aspxhome.com/file/2023/7/56047_0s.jpg)
PHP中Too few arguments to function的问题及解决
2023-06-04 21:15:55
![](https://img.aspxhome.com/file/2023/3/55303_0s.png)
微软建议的ASP性能优化28条守则(8)
2005-05-30 16:04:00
tensorflow将图片保存为tfrecord和tfrecord的读取方式
2022-01-22 11:32:04
![](https://img.aspxhome.com/file/2023/4/77004_0s.jpg)
JavaScript中尽量用局部变量的原因[译]
2009-02-20 13:45:00
![](https://img.aspxhome.com/file/UploadPic/20092/20/01-88s.jpg)
Python中dict和set的用法讲解
2023-12-09 05:20:55
![](https://img.aspxhome.com/file/2023/7/81507_0s.png)
python实现监控指定进程的cpu和内存使用率
2023-08-23 02:21:17
![](https://img.aspxhome.com/file/2023/3/62003_0s.jpg)
2011年网页设计发展趋势
2011-01-10 20:45:00
![](https://img.aspxhome.com/file/UploadPic/20111/10/01-45s.jpg)
ASP技巧:Script块不能放在另一个Script 块内
2009-08-19 17:17:00
python中的zip模块
2023-09-13 07:49:56
ASP.NET对路径"xxxxx"的访问被拒绝的解决方法小结
2023-07-07 20:08:41
Python实现自动登录百度空间的方法
2023-11-11 09:11:23
Python linecache.getline()读取文件中特定一行的脚本
2023-03-09 13:18:05
![](https://img.aspxhome.com/file/2023/5/67725_0s.gif)
数字人组件反写[asp组件开发实例5]
2009-06-09 13:23:00
![](https://img.aspxhome.com/file/UploadPic/20093/9/20093918286639s.gif)
Python 实现图像合成微缩效果
2023-08-19 22:15:16
![](https://img.aspxhome.com/file/2023/0/79440_0s.jpg)
搜索结果页(SERP):前言
2009-07-22 20:56:00
![](https://img.aspxhome.com/file/UploadPic/20097/22/01-73s.jpg)
如何将Python脚本打包成exe应用程序介绍
2021-12-26 20:42:07
![](https://img.aspxhome.com/file/2023/4/81724_0s.png)
详解Python的循环结构知识点
2021-09-30 11:42:03
Python 数字转化成列表详情
2023-09-24 06:53:25
![](https://img.aspxhome.com/file/2023/3/67373_0s.png)