作者scott0528 (Solar)
看板Python
标题[问题] List 回传None
时间Fri Apr 13 14:38:22 2018
请教前辈问题点,我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://webptt.com/cn.aspx?n=bbs/Python/M.1523601505.A.FCC.html
1F:推 Yshuan: addTwoNumbers这个function没有return value.. 04/13 14:47
2F:推 Yshuan: 所以a当然是拿到None 然後你又print(a) 04/13 14:55
3F:推 handsomeLin: You have already passed list into function, you c 04/13 14:56
4F:→ handsomeLin: an just get final result from that list. Also, yo 04/13 14:56
5F:→ handsomeLin: ur function doesn’t return any value, that’s wh 04/13 14:56
6F:→ handsomeLin: y it print None 04/13 14:56
7F:→ scott0528: 感谢前辈 04/13 14:57
8F:推 handsomeLin: BTW, this function can’t solve two lists with di 04/13 14:59
9F:→ handsomeLin: fferent lengths. 04/13 14:59