Python报错

一、关于缩进问题报错

>>> import trees
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import trees
  File "D:\Python\trees.py", line 9
    labelCounts[currentLabel] = 0
                                ^
TabError: inconsistent use of tabs and spaces in indentation
>>> import trees
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    import trees
  File "D:\Python\trees.py", line 9
    labelCounts[currentLabel] = 0
              ^
IndentationError: expected an indented block
>>> import trees
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    import trees
  File "D:\Python\trees.py", line 11
    shannonEnt = 0.0
                   ^
IndentationError: unindent does not match any outer indentation level
以上三种都是有关缩进问题的报错,第二段代码是由于没有缩进导致报错,;第一段和第三段是由于空格和tab混用……

所以要注意缩进问题,空格和tab不能混用。

二、“dict_keys”对象不支持索引

例子来源于《机器学习实战》决策树

def classify(inputTree, featLabels, testVec):
    firstStr = inputTree.keys()[0]                      #line 78
    secondDict = inputTree[firstStr]
    featIndex = featLabels.index(firstStr)
    for key in secondDict.keys():
        if testVec[featIndex] == key:
            if type(secondDict[key]).__name__ == 'dict':
                classLabel = classify(secondDict[key], featLabels, testVec)
            else:
                classLabel = secondDict[key]
    return classLabel
如果使用上面的决策树分类函数,报错:

>>> trees.classify(myTree, labels, [1,0])
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    trees.classify(myTree, labels, [1,0])
  File "D:\Python\trees.py", line 78, in classify
    firstStr = inputTree.keys()[0]
TypeError: 'dict_keys' object does not support indexing
这是因为Python2.x与3.x的差别导致的。这时我们可以用list(inputTree.keys())或者list(inputTree)来解决。

详细代码如下:

def classify(inputTree, featLabels, testVec):
    firstStr = list(inputTree.keys())[0]
    secondDict = inputTree[firstStr]
    featIndex = featLabels.index(firstStr)
    for key in secondDict.keys():
        if testVec[featIndex] == key:
            if type(secondDict[key]).__name__ == 'dict':
                classLabel = classify(secondDict[key], featLabels, testVec)
            else:
                classLabel = secondDict[key]
    return classLabel
>>> trees.classify(myTree, labels, [1,0])
'no'





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,报错反馈是程序运行过程中遇到问题时的重要组成部分。当代码执行出错,Python解释器会生成错误信息(也称为异常或错误消息),这些信息通常包含了出错的位置、类型以及关于错误原因的详细描述。理解这些错误信息有助于开发者定位和修复代码中的问题。 Python报错分为几种常见类型: 1. **语法错误**(SyntaxError):这是最基本的错误类型,通常由于拼写错误、缺少必要的符号或不符合Python语法规则导致,如括号不匹配或缩进错误。 2. **运行时错误**(RuntimeError):这类错误在程序执行过程中出现,比如除数为零、尝试访问不存在的键等,它们不会阻止程序编译,但会导致程序停止执行。 3. **类型错误**(TypeError):当操作符或函数应用于不兼容的数据类型时,例如字符串和整数相加。 4. **名称错误**(NameError):当你试图使用一个未定义的变量或函数名。 5. **ImportError**:当尝试导入一个模块而模块找不到时发生。 6. **KeyError**:在字典中查找不存在的键时。 7. **AttributeError**:对象没有预期的属性或方法。 要解决Python报错,你可以采取以下步骤: - 阅读错误消息,了解错误类型和位置。 - 使用`try-except`结构捕获并处理可能的错误。 - 使用`debugger`(如pdb)进行逐步调试。 - 查阅官方文档或在线资源,如Stack Overflow,寻求解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值