Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从倍数列表中计算乘积_Python_Python 3.x - Fatal编程技术网

Python 从倍数列表中计算乘积

Python 从倍数列表中计算乘积,python,python-3.x,Python,Python 3.x,我有一个Python赋值,它要求我创建一个函数,返回一个倍数数字列表。然后,编写另一个函数,获取数字列表并计算列表中所有项目的乘积。函数中必须使用For循环 输出应如下所示: Enter multiple of: 2 Enter an upper limit: 10 [2, 4, 6, 8, 10] product is 3840 但是我无法让第二个函数工作,它打印0 #from functools import reduce # Valid in Python 2.6+, required

我有一个Python赋值,它要求我创建一个函数,返回一个倍数数字列表。然后,编写另一个函数,获取数字列表并计算列表中所有项目的乘积。函数中必须使用For循环

输出应如下所示:

Enter multiple of: 2
Enter an upper limit: 10
[2, 4, 6, 8, 10] 
product is 3840
但是我无法让第二个函数工作,它打印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))
我得到的结果是:

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 输入以下倍数:2 输入上限:10 [0,2,4,6,8]我不知道如何加上限制器,但这还不是我关心的问题。 0不对 我试着按照这里的建议使用reduce,但我不知道我是否做了一些错误的事情,因为它不起作用。

Python的
range()
已经内置了这个功能:
range(start,stop,increment)

简单地说:

def func_list(multi,upper,a):
    a = list(range(multi, upper+1, a))
如果需要使用for循环:

def func_list(multi,upper,inc):
    for i in range(multi, upper+1, inc):
        a.append(i)
你的第二个产品功能确实有效。它打印0的原因是因为这一行:
用于范围内的i(上限):
。这将导致0附加到列表中,使产品为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))

我在product函数中所做的是从作为参数传递的列表中创建一个新列表。我们使用for循环来附加到新列表中。我们可以将新列表传递给np.array(),以便在for循环之后从列表创建一个数组。我们可以使用np.product()函数传递列表数组并返回产品的完全格式化版本

如果列表从0开始,请尝试:
对于范围(1,上限)中的i:
而不是for循环,您可以使用
reduce来计算数字列表的乘积:@stephernauch注意到:)谢谢Primusa。我必须删除范围内I(multi,upper+1,inc)的for循环中的第三个参数:因为它在我删除inc并替换为列表时给我造成了一个错误(无法解释列表)。然后,我添加了if语句以获得倍数,看起来它可以正常工作。
[2, 4, 6, 8, 10]
3840