CBSE Class 12 Computer Science Question 28 of 56

Functions — Question 28

Back to all questions
28
Question

Question 24

Write a Python function to multiply all the numbers in a list.

Sample List: (8, 2, 3, -1, 7)
Expected Output : -336

Solution
def multiply_list(numbers):
    product = 1
    for num in numbers:
        product *= num
    return product

numbers = eval(input("Enter the list: "))
product = multiply_list(numbers)
print("The product of the numbers in the list is", product)
Output
Enter the list: [8, 2, 3, -1, 7] 
The product of the numbers in the list is -336


Enter the list: [1, 6, 3, 5]  
The product of the numbers in the list is 90
Answer

def
multiply_list
(
numbers
):
product
=
1
for
num
in
numbers
:
product
*=
num
return
product
numbers
=
eval
(
input
(
"Enter the list: "
))
product
=
multiply_list
(
numbers
)
print
(
"The product of the numbers in the list is"
,
product
)
Output
Enter the list: [8, 2, 3, -1, 7]
The product of the numbers in the list is -336


Enter the list: [1, 6, 3, 5]
The product of the numbers in the list is 90