前言
在前面几篇我们把关于python基础语句了解学习了一下!今天我们分析一下,将会伴随我们整个python学习过程的boss!
是的! 就是-----报错!
对于前期学习 我们总结了大概10个常出的错误!
1,语法错误,非法的语法
name = '小王'
if name == '小王'
print('hello')
报错:
SyntaxError: invalid syntax
解决办法:看报错信息在第几行,从这一行往上找错误
2,语法错误2
count=0
while True:
count+=1
if count ==20:
return
报错:
SyntaxError: 'return' outside function
注意:return不能在方法以外用
解决办法:将return放在方法上
3,缩进错误
name = '小王'
for index in range(10):
if name == '小王':
print('hello')
else:
print('nothing')
报错:
IndentationError: expected an indented block
解决办法:tab自动缩进
4,字符串超出范围
content = 'hello world'
content = 'hello world'
print(content[21])
报错:
IndexError: string index out of range
解决办法:查看字符串长度,索引要小于长度
5,值错误
content = 'hello world'
result = content.index('a')
print(result)
报错
ValueError: substring not found
解决方法:仔细观察
6,索引错误(列表索引超出范围)
list1=['outMan','小李子','诺澜','皮克斯']
print(list1[0])
list1=['outMan','小李子','诺澜','皮克斯']
print(list1[5])
报错:
IndexError: list index out of range
解决办法:在范围内索引
7,属性错误
tp1.remove(1)
print(tp1)
报错:
AttributeError: 'tuple' object has no attribute 'remove'
解决办法:添加一个属性 供自己使用
8,键错误(没有指定的键值)
dic1 = {
'name':'张三',
'age':17,
'friend':['李四','王五','赵六','冯七']
}
print(dic1['fond'])
报错:
KeyError: 'fond'
解决报错:输入代码中有的键
9,类型错误
dic1.pop()
报错:
TypeError: pop expected at least 1 arguments, got 0
解决办法:给pop一个值
10,类型错误
name = '小王'
age=16
print('我的名字是'+name+'我的年龄是'+age)
报错:
TypeError: must be str, not int
解决办法: 使用加号拼接的时候,必须使用字符串,或者将数字转化成字符串 ,必须是字符串,不能是int型
好了! 今天我们就了解一下这10个错误!更多的麻烦还在后面等着我们!!加油!
ps:今天又又又又又又又又又是元气满满的一天! 明天继续干!!!