3/3 Product in Practice Make Perfect(french version), i receive a strange error


#1


https://www.codecademy.com/fr/courses/python-intermediate-fr-FR-py0yl/2/3?curriculum_id=5370f030fed2a866a2000001#

I try to create a function "produit" who take a list of numbers and return the product of all elements of the list. When i check my code, None is in the console as i expected but i receive this message :

Oups, merci de réessayer. Votre fonction a échoué sur [4, 5, 5] comme entrée car votre fonction indique une erreur "'int' object is not callable".

And this is my code.


nombre = [1,2,3,4,5,6]
def produit(nombre):
    total = 1
    for x in nombre:
        if x == 0:
            nombre.remove(0)
        else:
            total *= x
    return total

Thanks.


#2

Broken exercise that nobody intends to fix

Btw, removing from a list causes elements to move. You're iterating through that list, so unless it is made sure that the iterating correctly (whatever that means) handles moving elements, then don't iterate through and change the same list.


#3

Thanks for your answer iotanan. So, It is a bug of codecademy, what's means i will just go to the next exercise but I cannot complete the course to 100 %.


#4

You can crash the submission test by changing things it relies on, and that makes it pass because.. yeah no good reason.

# Add the following at the bottom of your code
del produit
NameError = Exception = None

#5

Thanks you. It pass, but i don't even understand why. Magic happens. How have you find that ?


#6

The submission test is in the page source, so it can be studied to come up with a way of crashing it.

Those two lines remove the variable that refers to your function and changes names that should refer to exception classes. When it doesn't find your function it will raise a NameError which it will try to catch. But because I changed the values of NameError and Exception, it is unable to catch the exception, so the submission test crashes because of the unhandled exception.

# Uncommenting the next line makes the except clause fail to catch the error
# NameError = None

try:
    non_existing_variable
except NameError:
    print 'this variable doesn\'t exist'

#7

I think i understand, you make the test fail by destroying the name of the function and block the exception to raise.

But why an error blocked make codecademy valid my code ?

I must study more the try and except concept, it's seems powerful.


#8

Just to be clear, the code you posted isn't correct. Try putting a zero at the beginning of the list, causing your function to remove it and thus make the other elements move.


#9

Ok, i change my code to remove 0 in the while loop, and i check my code in a console with some 0 in nombre list. The while loop remove all 0 to my list before the for loop.

I don't know if i am clear.

Here my code:

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

def produit(nombre):
    total = 1
    while 0 in nombre:
        nombre.remove(0)
    for x in nombre:
        total *= x
    return total

Is this correct ?


#10

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