Product, i pass when i feel i shouldnt


#1

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


I am able to multiply all the numbers in a list and return the sum, but if the list contains a 0 it returns 0, as the hint is warning you about, so i wanna try and fix that before moving on. I've tried ".remove()" but it doesnt seem to be executing. So can someone point out where my thinking fails? Would be much appreciated!

Thanks otherwise for a awesome community :slight_smile:

numbers = [0,1,2,3,4,5]
def product(numbers):
    n = 1
    for x in numbers:
        n *= x
        if x == 0:
            numbers.remove('0')
        
    return n
    
print product(numbers)

#2

If I understand correctly, your code is almost perfect, @tommisvrd.

The problem is WHEN the code is executed. You have the number being operated on in the list, but before the 0's in the list have been removed. So it will still become 0. So just move the if statement above the n *=x and put the n*=x in an else statement. Looks like this:

numbers = [0,1,2,3,4,5]
def product(numbers):
    n = 1
    for x in numbers:
    if x == 0:
        numbers.remove('0')
    else:        
        n *= x        
    return n
    
print product(numbers)

If I understood correctly, that should help you out. Good luck! :snake:


#3

Thank you for the answer! i made the changes you suggested and saw aswell that my if statement had "if numbers = 0: when it should be "if x = 0:" dont know if you forgot to mention that change but it did the trick! So thank you very much!

However i ran into another problem, would you maybe be able to help me with that aswell?
The problem is that when i run the code with the 0 as the first item in the list, example: [0,1,2,4,5] it removes the 0 and gives me the expected answer "120". But if i have the 0 in the code it like this: [1,2,0,3,4,5] it gives me either 30 or 40 depending on where in the list the 0 is. Do you have any idea why that could be?

Sorry for a bit of wall of text and again thanks!


#4

@tommisvrd

No problem. Remember to mark the question as answered by the appropriate response so other users know.

In addition:

In python if statements always need two = signs. Otherwise it's like declaring a variable. Declaring variables uses 1, if statements use 2. If the exercise accepted 1 it was written incorrectly, and you should report that bug.


#5

Hi friends thank you your code it really did help me through the process, However, I am experiencing the same issues.

integers = [1,2,0,3]
def product(integers):
    n = 1
    for x in integers:
        if x == 0:
            integers.remove(0)
        else:        
            n *= x        
    return n
print product(integers)

prints

2

But thanks it still passes the exercise :slight_smile:
Would just like to know a solution..


#6

Not sure but I think that the if loop exits as soon as it hits '==0'. The product is then 1x 2 and returned.
Imo the if x==0 should not be in here. Bc if 0 is in the list, the product SHOULD be zero...


#7

I understood it as, if you get a 0 in the list then the function should be able to detect it and remove it from the list so the product isnt 0.


#8

I just made an typo, i wrote "if x == 0:" in the code, but in my reply i forgot one "=" so no bug what i know of.


#9

The exercise didn't ask you to computer the product discounting 0, just compute the product. This could be 0 if 0 is in the list.

It mentions not initialise the variable to which you will return that product as 0, it must be set to 1.


#11

I believe their is no need for if or else in this , the following code works:

def product(numbers):
total=1
for i in numbers:
total*=i
return total