13. product stuck



gives me an error total reference before asignemt

product odf numbers

def product(numbers):
    for n in numbers:
        total *= int(n)
    return total


You need to add total = 1 before loop.


it works now thank's :slight_smile:


this is a really smart way of doing it. My way was a bit more complicated:

def product(numbers):     #Define a function
    result = 0                   #variable result set to 0
    for i in numbers:          # iterate through the the given list of numbers
        if result != 0:           # if the result is not 0
            result = result * i  # then multiply result by the number
            result = i             #if not then set result to being the number
    return result                 # return the result

but I can see now that I can could simply skip the entire part of my code that tries to make sure you don't multiply by 0 the first time by just setting the variable, my case 'result', to 1.

so thank you for this,

hope my logic here helps someone as well.


the good thing on coding is the freedom to do it your on way,
i see this code in the forum and adapeted to me .
yours is also good ,it works that's what matters :slight_smile:
what i have learn is if you can make your program with less lines better


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