简体   繁体   中英

Calculate the product from a list of multiples

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM