作者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://www.ptt.cc/bbs/Python/M.1523601505.A.FCC.html
推 Yshuan: addTwoNumbers這個function沒有return value.. 04/13 14:47
推 Yshuan: 所以a當然是拿到None 然後你又print(a) 04/13 14:55
推 handsomeLin: You have already passed list into function, you c 04/13 14:56
→ handsomeLin: an just get final result from that list. Also, yo 04/13 14:56
→ handsomeLin: ur function doesn’t return any value, that’s wh 04/13 14:56
→ handsomeLin: y it print None 04/13 14:56
→ scott0528: 感謝前輩 04/13 14:57
推 handsomeLin: BTW, this function can’t solve two lists with di 04/13 14:59
→ handsomeLin: fferent lengths. 04/13 14:59