亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

三引號字符串注釋崩潰簡單的python程序

三引號字符串注釋崩潰簡單的python程序

忽然笑 2021-12-09 16:00:17
當我嘗試向我的代碼添加注釋時,我發現了這個奇怪的問題。我使用三重引號字符串進行注釋,但程序因出現以下錯誤而崩潰: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'])
查看完整描述

3 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

罪魁禍首:


移動函數定義內的注釋:


原因:


由于三引號字符串是有效的 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


查看完整回答
反對 回復 2021-12-09
?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

作為注釋的三重引號字符串必須是有效的 Python 字符串。有效的 Python 字符串必須正確縮進。

Python 看到多行字符串,對其進行評估,但由于您沒有為其分配變量,因此該字符串在下一行中被丟棄。


查看完整回答
反對 回復 2021-12-09
?
largeQ

TA貢獻2039條經驗 獲得超8個贊

您應該將三引號字符串的縮進級別向右推一個標簽。

雖然三引號字符串經常被用作注釋,但它們是普通的 Python 表達式,因此它們應該遵循語言的語法。


查看完整回答
反對 回復 2021-12-09
  • 3 回答
  • 0 關注
  • 262 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號