python报错汇总
标签(空格分隔): 4.1python
-
D:\Python36\python.exe E:/PyCharm/pythonbase/day01/02_变量.py Traceback
(most recent call last): File
“E:/PyCharm/pythonbase/day01/02_变量.py”, line 12, in
print (name+age+hobby) TypeError: must be str, not int 类型错误:必须是字符串,不能是整数型 -
解决方法:将整数型前边加上str,转化为字符串类型
-
File “E:/PyCharm/pythonbase/day01/03_程序的输入与输出.py”, line 7
print(1,2,3,4,5,sep=’-’)
^ IndentationError: unexpected indent 缩进错误:意外缩进 -
解决:将print顶格/或4个空格
-
print(‘我本来有5元,现在一共有’,money+5,‘元’)
TypeError: must be str, not int
错误类型:此处5是int型 -
解决方法:print(‘我本来有5元,现在一共有’,str(5)+money,‘元’)
- 注意:
1.print(‘我本来有5元,现在一共有’,str(5)+money,‘元’)
输出:我本来有5元,现在一共有 5**(字符串拼接) 元
- 2.print(‘我本来有5元,现在一共有’,‘str(5)+money’,‘元’)
输出:我本来有5元,现在一共有 str(5)+money(不会对内容拼接,直接显示引号中的内容) 元
- print(‘我本来有5元,现在一共有’,5+money,‘元’)
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
错误类型:此处money是字符串
数字后面跟+,默认是做数学运算
解决方法:print(‘我本来有5元,现在一共有’,5+int(money),‘元’)
-
x=float(x)
ValueError: could not convert string to float: ‘1+1’
错误类型:1+1 不能由字符串转变为浮点型数值(原因:+号不能转换) -
解决方法:输入浮点型数值
-
print (students4)
IndexError: list index out of range -
错误类型:
索引错误:超出列表的索引范围(下标太大) -
解决方法:更改下标 或 增添索引的范围即添加内容
a[0]=‘H’
TypeError: ‘str’ object does not support item assignment
错误类型:字符串对象不支持项目分配
b[0]=‘H’
TypeError: ‘tuple’ object does not support item assignment
错误类型:元组对象不支持项目分配
TypeError: myprint() missing 1 required positional argument: ‘x’
错误类型:函数xmyprint缺失 运行所需的函数x 的值
解决方法: 添加x 的函数
AttributeError: ‘person’ object has no attribute ‘score’
属性错误:某个队形(person)缺失某个属性(score)
解决方法:添加属性
TypeError: write() argument must be str, not int
ValueError: I/O operation on closed file.
value错误:I/O 操作在关闭的文件上
文件被代码关闭,无法再写入
解决方法:将代码放置 file.close()函数之前
PermissionError: [Errno 13] Permission denied: ‘newExcel.xls’
权限错误:
原因:使用Excel或者WPS已打开某个文件,还想继续写入或者更改