Python multiplying all even numbers in a list
<p>I am working on this python code for my first programming class. Yesterday it partially worked but then I changed something and now it is only passing 1 test case. The goal is to multiply all even numbers in list "xs" and return 1 if there are no even numbers. What am I doing wrong and how can I fix it?</p>

<pre><code>def evens_product(xs):
    product = 2
    for i in xs:
        if i%2 == 0:
            product *= i
            return product

    else:
        return (1) 
</code></pre>

<p>Edit: Chepner's solution worked thank you to everyone who helped</p>

<p>You need to initialize <code>product = 1</code>, for two reasons. One, simply put, you'll get the wrong answer. <code>evens_product([4])</code> should return 4, not 8.
Two, it saves you from having to treat a list with no even numbers as a special case. If there are no even numbers, you never change the value of <code>product</code> and return it unchanged.</p>

<pre><code>def evens_product(xs):
    product = 1
    for i in xs:
        if i%2 == 0:
            product *= i
    return product
</code></pre>

python list numbers multiplying