How to write to achieve the purpose of multiplication? (1 * 2 * 5 * 9), thank you
Def mysum (l) :
Print (l)
If not l:
Return 0
The else:
Return [0] * mysum l (l [1])
Print (mysum (,2,5,9 [1]))
Code execution results:
[1, 2, 5, 9]
[2, 5, 9]
[5, 9]
[9]
[]
0
CodePudding user response:
Now that is the multiplication, there will be no return 0, finally to return 1
Def mysum (l) :
Print (l)
If not l:
Return 1
The else:
Return [0] * mysum l (l [1])
Print (mysum (,2,5,9 [1]))
CodePudding user response:
the from functools import reduce
Def f (ls) :
Return the reduce (lambda x, y, x * y, ls)
Print (f (,2,5,9 [1]))