python 遞迴調用 傳回值問題

來源:互聯網
上載者:User

標籤:color   data   div   通過   sam   blog   沒有   綠色   bsp   

當使用遞迴時並有傳回值時,調用自身函數時需要加上return語句如下:
def daxiao(biao1,biao2):    #判斷兩個列表的大小,根據裡面的元素大小    #如果biao1大於biao2,返回1,    #如果biao1小於biao2,返回0    #如果兩者相等,返回3    geshu1=len(biao1)    geshu2=len(biao2)     b1=int(biao1[0])    b2=int(biao2[0])  #這裡只能比較數值大小,不能通過字串直接比較大小    print(biao1)    print(biao2)    print(b1,b2,‘d‘)    if b1>b2 :        print(b1,b2)        return 1    elif b1<b2 :         return 0    else:        if(geshu1==geshu2==1):            print(‘same name‘)            return 3        elif(geshu1==1 and geshu2>1):            return 0        elif(geshu1>1 and geshu2==1):            return 1        else:            # a=daxiao(biao1[-geshu1+1:],biao2[1-geshu2:])           # return a   #上面兩句和這一句是等價的            return daxiao(biao1[-geshu1+1:],biao2[1-geshu2:])           這裡的return必須要加上,不然傳回值就是none,
因為調用函數時,是向裡傳輸資料,不加return時,就沒法往回傳輸資料。方法裡的return只是跳出被調用的那一層函數,當調到最外層時,語句是在調用的函數那裡,運行下去沒有return就返回None。所以必須在調用函數後加個return.要麼像綠色的那樣加兩句,要麼像紅色的直接返回就好。

 https://www.cnblogs.com/yechenkai/p/7143475.html

python 遞迴調用 傳回值問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.