栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

新手小白:python常见的错误原因及解决方法(完整版)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

新手小白:python常见的错误原因及解决方法(完整版)

解决方案

        先要给a赋值。才能使用它。在实际编写代码过程中 报NameError错误时 查看该变量是否赋值 或者是否有大小写不一致错误 或者说不小心将变量名写错了。上面两个代码的错误都是因为a变量并没有赋值。

        注 在Python中 无需显示变量声明语句 变量在第一次被赋值时自动声明。

 a 10
 print(a)
2.IndentationError代码缩进错误

IndentationError: expected an indented block

 x 10
 if x 10:
print(x)
IndentationError: expected an indented block

原因

        缩进错误 在这里需要强调一点 python不仅对大小写敏感 而且对缩进非常严格 行首多个空格 少个空格都会报错。这是新手常犯的一个错误 要引起注意。不仅if语句要缩进 def 函数 class 类 for 循环 while 循环 等代码块都需要缩进。

解决方案

 x 10
 if x 10:
 print(x)
3.AttributeError对象属性错误
 import math
 math.value
Traceback (most recent call last):
 File pyshell#14 , line 1, in module 
 math.value
AttributeError: module math has no attribute value 

原因 导入的math模块并没有values这个属性

解决方案

 import math
 math.sqrt(4)
2.0

python拓展 使用dir函数查看某个模块的属性

例如:

 dir(math)
[ __doc__ , __loader__ , __name__ , __package__ , __spec__ , acos , acosh , asin , asinh , atan , atan2 , atanh , ceil , comb , copysign , cos , cosh , degrees , dist , e , erf , erfc , exp , expm1 , fabs , factorial , floor , fmod , frexp , fsum , gamma , gcd , hypot , inf , isclose , isfinite , isinf , isnan , isqrt , lcm , ldexp , lgamma , log , log10 , log1p , log2 , modf , nan , nextafter , perm , pi , pow , prod , radians , remainder , sin , sinh , sqrt , tan , tanh , tau , trunc , ulp ]
4.TypeError类型错误

1 TypeError: list object cannot be interpreted as an integer

 t ( a , b , c )
 t [1,2,3,4]
 for i in range(t):
 print(s[i])
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/266726.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号