1.类型错误:必须为一个字符串 而非数字
name = '小张'
age = 17
print('我的名字是' + name +',我的年龄是' + age)
报错信息:TypeError: must be str, not int
2.语法错误:
if name == '小张'
print('Hello')
报错信息:SyntaxError: invalid syntax
3.缩进错误:未知缩进不匹配任何缩进等级
for index in range(10) :
if name == '小王' :
print('Hello')
else:
print('Nothing')
报错信息 :IndentationError: unindent does not match any outer indentation level
4.索引错误(1):索引范围不得超出字符串范围
content = 'hello world'
print(content [ 20 ])
报错信息:IndexError: string index out of rang
索引错误(2):索引范围不得超出列表范围
list = [ 'hello' , '诺拉 ' , 'hi ' ]
print( list [5] )
报错信息:IndexError: list index out of range
5.属性错误:元祖对象没有属性'remove'
tp1 = [ 1,2,3,4,'a',''b','c',Ture]
tp1.remove()
print(tp1)
报错信息:AttributeError: 'tuple' object has no attribute 'remove'
6.key 值错误:没有指定的键值 ' fond'
dic1 = {
'name' : '张三' ,
'age' : 14 ,
'friend' :[ '王二' ,'汤姆' ,'兰德' ]
}
print(dic1 (fond))
报错信息:TypeError: 'dict' object is not callable
7.类型错误:pop方法希望至少得到一个参数
dic1 . pop()
print(dic1)
报错信息:TypeError: pop expected at least 1 arguments, got 0
8.return的错误用法:不能在方法以外使用
count = 0
while Ture:
count += 1
if count == 20:
return
报错信息:SyntaxError: 'return' outside function
9.值错误:字符串未找到
content = ' hello world'
print( content[ 'a' ])
报错信息:ValueError: substring not found
以上就是今天学习发现的几处基础错误。