python学习常见的十种错误

在学习python的过程中,出现的错误是常见的,知错就改本身就是一个学习进步的过程,所以我们应该正视它。接下来我向大家介绍十种我在学习python过程中遇到的错误。

1.return没在方法中使用

# SyntaxError: 'return' outside function
# 解决:将return放在方法体中
# return不能在方法以外使用
# while True :
#     count += 1
#     if count == 20 :
#         return
2.类型错误
# name = '小王'
# age = 16
# TypeError: must be str, not int
# 类型错误 必须是一个字符串 不能是数字
# 解决办法:使用+拼接的时候 必须使用字符串,或者将数字转化成字符串
# print('我的名字是' + name + ',我的年龄是' + age)
3.语法错误
# SyntaxError: invalid syntax
# 语法错误 非法的语法
# 解决办法:看报错信息在第几行 ,从这一行往上找错误
# if name == '小王'
#     print('Hello')

4.缩进错误

# IndentationError: unindent does not match any outer indentation level
# indent 缩进错误 : 未知缩进不匹配任何缩进等级
# 解决办法:tab自动缩进
# name = '小王'
# age = 16
# for index in range(10):
#      if name == '小王':
#         print('hello')
#     else:
#      print('nothing')
5.索引错误
content = 'hello world'
#IndexError: string index out of range
# 索引错误:字符串超出了范围
# 解决办法:查看字符串的长度 索引要小于长度
# print(content[21])
6.值错误
# content = 'hello world'
# ValueError: substring not found
# 值错误:子字符串未找到
# result = content.index('r')
# print(result)
7.索引错误
# list1 = ['outMan','小李子','诺兰','皮克斯']
# IndexError: list index out of range
# 索引错误:列表索引超出范围
# print(list1[5])
8.属性错误
# tp1 = ((),[],{},1,2,3,'a','b','c',3.14 ,True)
# AttributeError: 'tuple' object has no attribute 'remove'
# attribute 属性 object对象
# 属性错误:元组对象没有属性'remove'
# tp1.remove(1)
# print(tp1)
9.键错误
# dic1 = {
#     'name': '张三',
#     'age' : 17 ,
#     'friend':['李四','王五','赵六','冯七']
# KeyError: 'fond'
# key 键错误 没有指定的键值“fond”
# print(dic1['fond'])
10.类型错误
# dic1 = {
#     'name': '张三',
#     'age' : 17 ,
#     'friend':['李四','王五','赵六','冯七']
# TypeError: pop expected at least 1 arguments, got 0
# arguments 参数   expected期望  at least 至少
# 类型错误:pop方法希望得到至少一个参数,但是现在参数为0
# dic1.pop()
# pop里面需要写参数 参数为想要删除的key值
未完待续!!!!










猜你喜欢

转载自blog.csdn.net/zuo199606184810/article/details/80992504
今日推荐