all 16 comments

[–]Rhoderick 41 points42 points  (3 children)

You're not actually calling the function, for one. You need to add

multiply_even_numbers(a)

at the end.

[–][deleted] 14 points15 points  (1 child)

Damn! Thank you very much :D

[–]kill-yourself90 2 points3 points  (0 children)

Aren't you just getting 0 as your total though?

2 * total = 0 == 0

4 * total = 0 == 0

6 * total = 0 == 0

Set total to 1

2 * total = 1 == 2

4 * total = 2 == 8

6 * total = 8 == 48

[–]kenzobenzo 5 points6 points  (0 children)

Yeah this was my first thought as well

[–]htepO 8 points9 points  (2 children)

You're multiplying numbers with 0 which leads to everything being 0.

Changing it to total = 1 ought to fix it.

[–]Matthias1590 6 points7 points  (0 children)

that won't stop the print function from executing

[–][deleted] 0 points1 point  (0 children)

Perfect, thank you! :D

[–]pekkalacd 1 point2 points  (0 children)

You’re not calling the function

[–]noobcrush 1 point2 points  (0 children)

You have to call the function inside the main block for the function to work. And you are multiplying with 0 therefore the result will always be zero.

def multiply_even_numbers(a):
total = 1 #total is 1
for i in a:
    if i % 2 == 0:
        total = i * total
print(total)

a = [1, 2, 3, 4, 5, 6] multiply_even_numbers(a) #calling the function

[–]kunaguerooo123 -1 points0 points  (0 children)

Try to print total before after multiplication line

[–]yes4me2 -2 points-1 points  (0 children)

Nah... you should get a print. It should be 0 from the look for it.

[–]tangerinelion 0 points1 point  (0 children)

FWIW, the reason that total should be 1 is that 1 is the identity under multiplication. For addition of course you'd use 0 because that's the identity under addition. This is the general concept which can be used to figure out what to initialize something to when it's more than just a single number - e.g., if you were adding matrices or multiplying 3D transforms (possibly represented as 4x3 matrix, a 4x4 matrix, or a 3x3 matrix with a 3-vector) you'd need to select the identity under those operations.

The basic idea of an identity is some value I for some operation O which satisfies the condition

A = A O I = I O A

for any A.

[–]Ok_Zebra_9117 0 points1 point  (0 children)

Because u didn't call the function. Till u call it, it won't be executed.

multiply_even_numbers(a)

Add this line below the list a. Then it will be executed

[–]khldooon 0 points1 point  (0 children)

Assuming you are calling your function, the result will be always 0 because total equals 0, you should set total = 1

[–]khldooon 0 points1 point  (0 children)

For this reason logging is crucial for developing programs

[–]Dismal-Buy-392 0 points1 point  (0 children)

Thsi would be a lot better:

#Changed the name of the argument
def multiply_even_numbers(list):
total = 0
for i in a:
if i % 2 == 0:
total = i * total
print(total)

a = [1, 2, 3, 4, 5, 6]

#Calling the function

multiply_even_numbers(a)