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

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

python 3 typeError:'NoneType'對象不可迭代

python 3 typeError:'NoneType'對象不可迭代

慕碼人8056858 2021-09-28 17:45:34
我寫了下面的代碼來找到*args的繼承。但我有一個錯誤。我真的不明白這個錯誤,因為我有輸入,但他們不是沒有。def third(*args, option=True):    if len(args) == 2:        word1, word2 = args    else:        word1 = args[0]    if option:        return word1, word2    else:        return word1def hello(data, *args, option=True):    print("the data is:", data)    A, B = third(*args, option=True)    print("the args are:", A, B)def world(small, *args, option=True):    return hello(small, *args)if __name__ == "main":    world("data","prediction")輸出:the data is: dataTraceback (most recent call last):File "<stdin>", line 1, in <module>File "<stdin>", line 2, in worldFile "<stdin>", line 3, in helloTypeError: 'NoneType' object is not iterable
查看完整描述

2 回答

?
斯蒂芬大帝

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

試試這個,應該工作:


def third(*args):

    if len(args) == 2:

        word1, word2 = args

        option = True

    else:

        option = False

        word1 = args[0]


    if option:

        return word1, word2

    else:

        return word1, None



def hello(data, *args):

    print("the data is:", data)

    A, B = third(*args)

    print("the args are:", A, B)


def world(small, *args):

    return hello(small, *args)



if __name__ == "__main__":

    world("data","prediction")


查看完整回答
反對 回復 2021-09-28
?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

基本上你正在傳遞 options=True 這意味著“第三個”函數應該總是返回 word1 和 word2。但是 args 的 len 是一個,因此根據您的 if len(args) == 2 條件,word2 不存在。

所以“第三個”函數只返回word1。在您的“hello”函數中,您試圖通過 A, B =third(arguments) 方法將該單個元素映射到兩個變量“A”和“B”,該方法迭代函數的返回值,但它無法這樣做,因為第三是返回一個元素或一個錯誤值(因為您試圖返回不存在的 word2)。這就是發生此錯誤的原因


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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