Factorial - message error but my code works well


#1



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


My code works well but I've got a message error:
"global name 'reponse' is not defined" error


Could you help with that please?

Note that I'm using the french version.


def factorielle(x):
    total = 1
    for i in range(x):    
        total *= (x - i)
    return total
    
print factorielle(0)


#2

this exercise is bugged, your code is fine.

Try this:

def reponse(x):
    if x < 2:
        return 1
    else:
        return x * reponse(x - 1)
def factorielle(x):
    total = 1
    for i in range(x):    
        total *= (x - i)
    return total
    
print factorielle(0)

it should work

or something a little shorter:

def factorielle(x):
    total = 1
    for i in range(x):    
        total *= (x - i)
    return total
reponse=factorielle
print factorielle(0)

#3

yes, that worked, thanks!


#4

i added a shorter solution:

def factorielle(x):
    total = 1
    for i in range(x):    
        total *= (x - i)
    return total
reponse=factorielle
print factorielle(0)

this would allow you to cheat the entire exercise together (abusing the bug in validition):

def factorielle(x):
    return x
reponse=factorielle
print factorielle(0)

anyway, you understand it (that is the most important bit). And now with a little hacking you got around the exercise as well. Well done :slight_smile:


#6