解决方案
先要给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])



