[問題] List 回傳None

作者: scott0528 (Solar)   2018-04-13 14:38:22
請教前輩問題點,我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)
作者: Yshuan (倚絃)   2018-04-13 14:47:00
addTwoNumbers這個function沒有return value..所以a當然是拿到None 然後你又print(a)
作者: handsomeLin (DoGLin)   2018-04-13 14:56:00
You have already passed list into function, you can just get final result from that list. Also, your function doesn’t return any value, that’s why it print None
作者: scott0528 (Solar)   2018-04-13 14:57:00
感謝前輩
作者: handsomeLin (DoGLin)   2018-04-13 14:59:00
BTW, this function can’t solve two lists with different lengths.

Links booklink

Contact Us: admin [ a t ] ucptt.com