In: Computer Science
Please what is wrong with this two code that is showing error message when run them?
>>>data =[1,2,3,4] >>>reduce(lambda x,y:x+y,data) #Produce the sum 10 >>> reduce(lambda x,y:x*y, data) # Produce the product 24
___________________________________________________________________________________________________________-
def sum(lower,upper): “””Returns the sum of the numbers from lower to upper.””” if lower> upper: return 0 else: return reduce(lambda x,y:x+y, range(lower, upper+1))
// Screenshot of the code
// Sample output
// Code to copy
sum.py
from functools import reduce
def sum(lower,upper):
if lower> upper:
return 0
else:
return reduce(lambda x,y:x+y, range(lower, upper+1))
Explanation:
You need to add the below module to your python program.
from functools import reduce