1.
name = '小王'
age = 16
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.
for index in range(10):
if name == '小王':
print('hello')
else:
print('nothing')
错误提示:IndentationError: expected an indented block
中译:缩进错误:期望缩进代码块
解决办法:用tab键让if语句缩进;如果是多行代码需要缩进,可以选中然后按tab键,会全部往后缩进。
5.
content = 'hello world'
print(content[21])
错误提示:IndexError: string index out of range
中译:索引错误 字符串超出了范围
解决办法:查看字符串的长度,索引要小于长度
6.
tp1 = ((),[],{},1,2,3,'a','b','c',3.14 ,True)
tp1.remove(1)
print(tp1)
错误提示:AttributeError: 'tuple' object has no attribute 'remove'
中译:属性错误 元组对象没有属性'remove'
解决办法:元组不能进行增、删、改等操作
content = 'Hello World'
result = content.index('100')
print(result)
错误提示:ValueError: substring not found
中译:值错误:子字符串未找到
解决办法:这个方法如果找不到指定的字符串会报错,用的时候要注意
8.dic1 = {
'name':'张三',
'age':17,
'friend':['李四','王五']
}
print(dic1['fond'])
错误提示:KeyError: 'fond'
中译:key 键错误:没有指定的键值“fond”
解决办法:可以给这个字典添加键值
dic1['fond'] = '学习'
print(dic1)
9.
dic1 = {
'name':'张三',
'age':17,
'friend':['李四','王五']
}
dic1.pop()
print(dic1)
TypeError: pop expected at least 1 arguments, got 0
类型错误:pop方法希望得到至少一个参数,但是现在参数为0解决办法:pop里面需要写参数,参数为想要删除的key值
dic1.pop('friend')
print(dic1)