【问题标题】:triple-quoted strings comments crash simple python program三引号字符串注释使简单的python程序崩溃
【发布时间】:2019-03-19 11:01:27
【问题描述】:

当我尝试将 cmets 添加到我的代码中时,我发现了这个奇怪的问题。我使用三引号字符串进行注释,但程序因给出以下错误而崩溃:

IndentationError: unexpected indent

当我使用# 注释三引号字符串时,一切正常。有谁知道这个错误背后的原因以及我该如何解决?

我的代码:

#This programs show that comments using # rather than """ """

def main():
    print("let's do something")
#Try using hashtag to comment this block to get code working
'''
    Note following block gives you a non-sense indent error
    The next step would be to consider how to get all the words from spam and ham
    folder from different directory. My suggestion would be do it twice and then
    concentrate two lists

    Frist think about the most efficient way
    For example, we might need to get rid off the duplicated words in the beginning

    The thoughts of writing the algorithem to create the dictionary

    Method-1:
    1. To append all the list from the email all-together
    2. Eliminate those duplicated words

    cons: the list might become super large

    I Choose method-2 to save the memory
    Method-2:
    1. kill the duplicated words in each string
    2. Only append elements that is not already in the dictionary

    Note:
    1. In this case, the length of feature actually was determined by the
    training cohorts, as we used the different English terms to decide feature

    cons: the process time might be super long
'''
    def wtf_python(var1, var2):
        var3 = var1 + var2 + (var1*var2)
        return var3

    wtfRst1 = wtf_python(1,2)
    wtfRst2 = wtf_python(3,4)

    rstAll = { "wtfRst1" : wtfRst1,
               "wtfRst2" : wtfRst2
    }
    return(rstAll)

if __name__ == "__main__":
    mainRst = main()
    print("wtfRst1 is :\n", mainRst['wtfRst1'])
    print("wtfRst2 is :\n", mainRst['wtfRst2'])

【问题讨论】:

  • def wtf_python(var1, var2): 不好玩吧?
  • 为您的 cmets 添加缩进
  • 我不得不承认。缩进错误让我很烦。多谢你们。这真的节省了我的一天。干杯
  • @TomGauss 发生在我们所有人身上,您可能会接受有帮助的答案。干杯!

标签: python debugging console comments python-3.6


【解决方案1】:

罪魁祸首

在函数定义中移动 cmets:

原因

由于三引号字符串是有效的 python exp,它们应该被同等对待,即在函数范围内。

因此

def main():
    print("let's do something")
    #Try using hashtag to comment this block to get code working
    '''
        Note following block gives you a non-sense indent error
        The next step would be to consider how to get all the words from spam and ham
        folder from different directory. My suggestion would be do it twice and then
        concentrate two lists

        Frist think about the most efficient way
        For example, we might need to get rid off the duplicated words in the beginning

        The thoughts of writing the algorithem to create the dictionary

        Method-1:
        1. To append all the list from the email all-together
        2. Eliminate those duplicated words

        cons: the list might become super large

        I Choose method-2 to save the memory
        Method-2:
        1. kill the duplicated words in each string
        2. Only append elements that is not already in the dictionary

        Note:
        1. In this case, the length of feature actually was determined by the
        training cohorts, as we used the different English terms to decide feature

        cons: the process time might be super long
    '''
    def wtf_python(var1, var2):
        var3 = var1 + var2 + (var1*var2)
        return var3

    wtfRst1 = wtf_python(1,2)
    wtfRst2 = wtf_python(3,4)

    rstAll = { "wtfRst1" : wtfRst1,
               "wtfRst2" : wtfRst2
    }
    return(rstAll)

if __name__ == "__main__":
    mainRst = main()
    print("wtfRst1 is :\n", mainRst['wtfRst1'])
    print("wtfRst2 is :\n", mainRst['wtfRst2'])

输出

let's do something
wtfRst1 is :
 5
wtfRst2 is :
 19

【讨论】:

    【解决方案2】:

    您应该将三引号字符串的缩进级别推到右侧一个标记。

    虽然三引号字符串经常被用作cmets,但它们是普通的python表达式,所以它们应该遵循语言的语法。

    【讨论】:

      【解决方案3】:

      作为 cmets 的三引号字符串必须是有效的 Python 字符串。有效的 Python 字符串必须正确缩进。

      Python 看到多行字符串并对其求值,但由于您没有为其分配变量,因此该字符串在下一行中被丢弃。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-29
        • 1970-01-01
        • 2021-10-08
        • 1970-01-01
        • 1970-01-01
        • 2012-08-12
        • 2020-09-23
        • 1970-01-01
        相关资源
        最近更新 更多