I splited and converted string into int and i want use the mul
operator to multiply directly. But they is an error when printing the last output.
from operator import mul
# mulptiply user input
input_string = input("Enter numbers for summing :")
print("/n")
user_list = input_string.split()
print('list:', user_list)
# for loop to iterate
for x in range(len(user_list)):
user_list[x] = int(user_list[x]
# calc the mul of the list
print("Multiplication of list =", mul(user_list))
The mul
function takes 2 arguments at a time and returns their product. If you want to use mul
to calculate the product of a list of numbers you can use functools.reduce
to apply the function cumulatively to every list item for an aggregated result:
from functools import reduce
from operator import mul
user_list = [2, 3, 4]
print(reduce(mul, user_list)) # outputs 24
Beginning in Python 3.8 you can also use math.prod
to calculate the product of a list of numbers directly.