python常见的几种基础错误

IndexError: string index out of range\

这是索引错误:超出范围的字符串索引,如果字符串没有这么长,却索引很长,就会报这样的错,错误代码如下:

content='hello world'

print(content[20])

ValueError: substring not found

这是值错误,表示字符串未找到,一般想在一串字符串中用index搜索一个字符的位置如果没找到就会报这样的错误

代码如下:

content='hello world'
result=content.index('l')

list index out of range

这是索引错误,列表索引超出范围,代码如下

list1=['outMan','小李子','诺兰','皮克斯']

 print(list1[7]
'tuple' object has no attribute 'remove'

这是指元组没有remove这个方法,因为元组是不允许修改的,只能查看,代码如下:

tp1=((),[],{},1,2,3,'a','b','c',3.14,True)
tp1.remove(1)
KeyError: 'fond'
这是指字典查询输入的key值错了,字典中没有fond这个key值,错误代码如下
dic1={
    'name':'张三',
    'age':17,
    'friend':['李四','王五','赵六','冯七']
}
print(dic1['fond'])
TypeError: pop expected at least 1 arguments, got 0

这里是指pop()函数里必须要至少有一个参数,但是它得到了0个参数,所以出错了,代码如下:

dic1.pop()

TypeError: must be str, not int

字符串和整型不能相加

SyntaxError: invalid syntax
语法错误,输入了错误的格式

 
 

 
 

猜你喜欢

转载自blog.csdn.net/qq_37958990/article/details/80991349