0

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.

karel
  • 5,489
  • 46
  • 45
  • 50
Sammy
  • 25
  • 2
  • You a list starts at 0, try: `for i in range (1, upper):` – Stephen Rauch Apr 18 '18 at 01:09
  • Instead of a for loop, you can calculate the product of a list of numbers with `reduce`: https://stackoverflow.com/questions/595374/whats-the-python-function-like-sum-but-for-multiplication-product – Gillespie Apr 18 '18 at 01:27

2 Answers2

0

Python's range() already has this functionality built in: range(start, stop, increment)

Simply:

def func_list(multi,upper,a):
    a = list(range(multi, upper+1, a))

If you need to use a for loop:

def func_list(multi,upper,inc):
    for i in range(multi, upper+1, inc):
        a.append(i)

Your second product function does actually work. The reason why it is printing 0 is because of this line: for i in range (upper):. This results in 0 getting appended to your list, making the product 0.

Primusa
  • 13,136
  • 3
  • 33
  • 53
  • @StephenRauch noted :) – Primusa Apr 18 '18 at 01:15
  • Thank you Primusa. I had to remove the third parameter in the for loop for i in range(multi, upper+1, inc): as it caused an error for me (list cannot be interpreted) as I removed inc and replaced with the list. I then added my if statement to get multiples and looks like its working. – Sammy Apr 18 '18 at 03:32
0
import numpy as np

def multiples_list(numbers, upper):
    '''
    Create a function that returns a list of multiples
    with a upper limit of 10.
    '''
    numbers_list = []

    for number in numbers:
       if number <= upper and number % 2 == 0:
           numbers_list.append(number)


    return numbers_list 


def product_of_multiples(numbers):

    new_numbers = []
    for num in numbers:
        new_numbers.append(num)

    numbers_array = np.array(new_numbers)

    product = np.product(numbers_array)

    return product


#numbers_list = list(int(input('Please enter a series of numbers: ')))

numbers_list = [2, 4, 6, 8, 10]

print(multiples_list(numbers_list, 10))

print(product_of_multiples(numbers_list))

Here is the output:

[2, 4, 6, 8, 10]
3840

What I did here in your product function is create a new list from the list that you pass as the argument. We use a for loop to append to the new list. We can pass the new list to np.array() to create an array from the list after the for loop. We can use the np.product() function and pass the array of the list and return a fully formatted version of the product.

Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27