13. Product help


#1




"invalid literal for int() with base 10: '['" error.


I made this function. It makes the correct answer but I keep getting the error message above.


def product(numbers):
    numbers = str(numbers)
    multiply = 1
    for digit in numbers:
        digit = int(digit)
        multiply *= digit 
    
    return multiply


#2

Define a function called product that takes a list of integers as input and returns the product of all of the elements in the list.

If we are to assume that numbers is a list, then we cannot convert it to a str. We do not need to create another iterable. The list is one, already. And they are all numbers, supposedly. Work from that notion.


#3

Ok thanks. I will keep this post updated if I cant figure it out.


#4

Yeah your right. My function was correct, I removed the "str" string and it worked out just fine. Thanks! I will post the code for those who are struggling with this as well.

def product(numbers):
    multiply = 1
    for digit in numbers:
        digit = int(digit)
        multiply *= digit 
    
    return multiply

#5

We're also given to assume that the list is all integers so that line is window dressing.

Let us know when you get to lambda in your studies. There is a use for it in this problem.


#6

Will do. Thanks for your help. Hope to see you again whenever I need help!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.