python常见错误

目录

join添加不成功:

TypeError: 'NoneType' object is not subscriptable无返回值​

SyntaxError:can't assign to function call不能分配给函数调用

invalid syntax,意思是语法错误

TypeError: xxx() takes no arguments

Python 中是没有 ++ 和 -- 的。那么要实现自增和自减的话,

list assignment index out of range

ValueError: invalid literal for int() with base 10: '2.3'

ValueError: Precision not allowed in integer format specifier

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

数学计算错误

TypeError: 'NoneType' object is not iterable

TypeError: 'map' object is not subscriptable

TypeError: 'float' object cannot be interpreted as an integer

深复制浅复制,列表赋值后两个列表同时发生改变

RecursionError: maximum recursion depth exceeded while calling a Python object

TypeError: cannot unpack non-iterable NoneType object

TypeError: list indices must be integers or slices, not tuple

Python中and和or:

unexpected EOF while parsing


join添加不成功:

[1,3,5].join(‘,’)原理:1 + ‘,’ + 2+ ’,’ + 3 + ‘,’   题目中“,”.join(ls)列表中的数字字符串“,”相加,

不可以使用关键字,(关键字与内置函数名)

 列表类型不能使用等号进行复制,两个变量名共享一个列表

Ls = [‘a’,’b’,’c’]

Ls[1]   是字符串           ls[1:2]    是列表【列表的切片仍然是列表】

TypeError: 'NoneType' object is not subscriptable无返回值

 N.append(0)是无返回值的,append()方法无返回值,但是会修改原来的列表。

但是我把N.append0)赋值给了S,导致最终S的值为None

SyntaxError:can't assign to function call不能分配给函数调用

函数方法书写错误

operandStack是我想定义的变量,结果我在变量名后面加了括号

invalid syntax,意思是语法错误

1、符号使用不正确

例如:只写了半个括号,使用了中文符号等。

2、代码缩进问题

python如果缩进不正确或缩进格式不统一,一般信息会明确告诉,但有时也会出现invalid syntax报错。python的缩进是四个空格或一个TAB,如果缩进三个空格,一定报错。

TypeError: xxx() takes no arguments

1. "_ _"为双下划线,而非单下划线"_";

2. "init"的拼写。

Python 中是没有 ++ 和 -- 的。那么要实现自增和自减的话,

可以使用如下操作:

a = a + 1

a += 1

list assignment index out of range

可能情况是:list[index]index超出范围

list是一个空列表,没有一个元素,进行list[0]就会出现错误!

ValueError: invalid literal for int() with base 10: '2.3'

假如字符串 s 是一个浮点数形式的字符串,比如 '-3.14',直接用 int 进行类型转换会报 ValueError 

如果想要把浮点数形式的字符串转换为整数,需要先使用 float 把字符串转换为浮点数,再对该浮点数使用 int,把浮点数截取为整数,如下所示:

ValueError: Precision not allowed in integer format specifier

不允许在整数格式说明符中使用精度

 

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

诸如此类错误,表示两个类型不同的变量,不可用用“+”或“-”来进行运算。

使用python中的转换函数将他们转换成一样的类型才可用。

数学计算错误

数学计算最好还是用函数,直接输入计算容易出错【最好全部加上括号】

TypeError: 'NoneType' object is not iterable

ls.sort(reverse = True)该方法没有返回值

这个错误提示一般发生在将None赋给多个值时

在判断语句中,当if条件不满足,并且没有else语句时,函数默认返回None。

在没有return语句时,Python也默认会返回None

调用时,将None赋给多个值时,会出现提示:TypeError: 'NoneType' object is not iterable

TypeError: 'map' object is not subscriptable

Map没有下标的属性,

 

TypeError: 'float' object cannot be interpreted as an integer

而在python3中,整数 / 整数 = 浮点数 也就是100 / 2 = 50.0 不过,使用 '//'就可以达到原python2'/'的效果。

可以进行强制转换,把浮点型转变成整型,也可以直接使用'//'进行运算。

深复制浅复制,列表赋值后两个列表同时发生改变

浅复制和深复制的区别python

RecursionError: maximum recursion depth exceeded while calling a Python object

没有递归出口

TypeError: cannot unpack non-iterable NoneType object

解决方法:报错的原因是函数返回值得数量不一致,查看函数返回值数量和调用函数时接收返回值的数量是不是一致,修改一致即可

TypeError: list indices must be integers or slices, not tuple

列表索引必须是整数(取某一个数据,例如:data[0])或者片(取某一片数据,例如data[0:2]),不能是元组(tuple是元组的意思)

列表取片操作易错点(list indices must be integers or slices, not tuple原因及解决方法客

Python中and和or:

在python里面,0、’’、[]、()、{}、None为假,其它任何东西都为真。

1not True = False 或者 not False = True (非真就是假,非假即真)

2and是一假则假,两真为真,两假则假

3or是一真即真,两假即假,两真则真

优先级是 not > and > or

对于and,如果没有假值,返回的是最后一个真值,如果有假值,则返回的是第一个假值。

对于or,如果没有真值,返回的是最后一个假值,如果有真值,则返回的是第一个真值。

Python运算符优先级和结合性一览表 (biancheng.net)

 

unexpected EOF while parsing

一般原因是:

1 语法有问题,一般都是少了print最后的一个括号;

2 输入语法有问题,有时候忘了切换中英文输入法,可以删除了重新输入。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值