[問題] List 回傳None

看板Python作者 (Solar)時間6年前 (2018/04/13 14:38), 編輯推噓4(405)
留言9則, 3人參與, 6年前最新討論串1/1
請教前輩問題點,我print出來的值都會有None。試過有給temp list 初始值[0,0],結果 還是會多個None。還請前輩們指點。此問題是從Leetcode 第二題 addTwoNumber出來的。 def addTwoNumbers( l1, l2,temp): carry = 0 for i in range(len(l1)): if (l1[i]+l2[i])+carry>=10: if carry==0: temp.append((l1[i]+l2[i])%10) carry=1 else: temp.append((l1[i]+l2[i])%10+carry) carry=1 else: temp.append((l1[i]+l2[i])%10+carry) carry=0 if carry==1: temp.append(1) print(temp) list1=[1,1] list2=[1,1] list=[] a=addTwoNumbers(list1,list2,list) print(a) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.136.149.11 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1523601505.A.FCC.html

04/13 14:47, 6年前 , 1F
addTwoNumbers這個function沒有return value..
04/13 14:47, 1F

04/13 14:55, 6年前 , 2F
所以a當然是拿到None 然後你又print(a)
04/13 14:55, 2F

04/13 14:56, 6年前 , 3F
You have already passed list into function, you c
04/13 14:56, 3F

04/13 14:56, 6年前 , 4F
an just get final result from that list. Also, yo
04/13 14:56, 4F

04/13 14:56, 6年前 , 5F
ur function doesn’t return any value, that’s wh
04/13 14:56, 5F

04/13 14:56, 6年前 , 6F
y it print None
04/13 14:56, 6F

04/13 14:57, 6年前 , 7F
感謝前輩
04/13 14:57, 7F

04/13 14:59, 6年前 , 8F
BTW, this function can’t solve two lists with di
04/13 14:59, 8F

04/13 14:59, 6年前 , 9F
fferent lengths.
04/13 14:59, 9F
文章代碼(AID): #1Qq51X_C (Python)