I have a Python assignment where it asks me to create a function that returns a list of numbers that are multiples. Then, write another function that takes the list of numbers and calculates the product of all the items in the list. For loop must be used in your function.
The output should be like:
Enter multiple of: 2
Enter an upper limit: 10
[2, 4, 6, 8, 10]
product is 3840
but I cannot get the second function to work, it prints 0.
#from functools import reduce # Valid in Python 2.6+, required in Python 3
#import operator
a = []
def func_list(multi,upper,a):
for i in range (upper):
if i % multi == 0:
a.append(i) #DOESNT INCLUDE THE UPPER LIMIT
multi = int(input("Enter multiple of: "))
upper = int(input("Enter an upper limit: "))
func_list(multi,upper,a)
print(a)
#b
#input = list of number (param)
#output = calculates the product of all the list (sum)
def prod(a):
prod1 = 1
for i in a:
prod1 *= i
return prod1
#return reduce(operator.mul, a, 1)
#func_list(multi,upper)
prod(a)
print (prod(a))
The output I get is:
Enter multiple of: 2 Enter an upper limit: 10 [0, 2, 4, 6, 8] I don't know how to inc. the limiter, but it's not my concern yet. 0 not right
I tried using reduce as suggested on here, but I don't know if I did something incorrect, because it didn't work.