13. product stuck


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/3?curriculum_id=4f89dab3d788890003000096#

gives me an error total reference before asignemt

product odf numbers


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


#2

You need to add total = 1 before loop.


#3

it works now thank's :slight_smile:


#4

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
        else:                       
            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.


#5

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


#6

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