[问题] 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