5. Factor - Error: Failed with 1 as entry (global name 'reponse' is not defined)

Hi,

I have a bit of a problem on the French version of this exercice (15.5 Factor), I keep getting the same error:
Oups, merci de réessayer. Votre fonction a échoué sur 1 comme entrée car votre fonction indiqueOups, merci de réessayer. Votre fonction a échoué sur 1 comme entrée car votre fonction indique “global name ‘reponse’ is not defined” error.
Oops, please try again. Your function failed with 1 as entry because you function state “global name ‘reponse’ is not defined” error.

I tried most of the function that are on this Discuss community Forums. Even when I import the math formula it send back the same error.

Does anyone can’t get past that too?

For information, here is the formula that work in the CodecademyLab:

def factor(n):
----if n > 1:
--------return n * factor(n - 1)
----elif n < 1:
--------return -n * factor(-n - 1)
----elif n == 0:
--------return 1
----else:
--------return 1

x = raw_input("Nombre pour factorielle? ")
print factor(int(x))

Thank you

That’s strange. Try using a variable to hold the factor.

def factor(n):
----if n > 1:
--------answer = n * factor(n - 1)
----elif n < 1:
--------answer = -n * factor(-n - 1)
----elif n == 0:
-------- answer = 1
----else:
--------answer = 1
----return answer

x = raw_input("Nombre pour factorielle? ")
print factor(int(x))

1 Like

What did you enter when testing it?

1 Like

Please post a link to the exercise. Thank you.

1 Like

Hello hello,

To a_distraction:
I entered positive and negative integrer (such a -7 or 11 for ie) but none of them worked anyway…
I just tried your method but it get back at me with:
Oops, please try again. Your function failed with 1 as entry because you function state “global name ‘reponse’ is not defined” error

To mtf:
Here’s a link:
https://www.codecademy.com/fr/courses/python-intermediate-fr-FR-py0yl/0/5?curriculum_id=5370f030fed2a866a2000001

I tried the solutions your proposed on the other discussion by the way, but there must something I do wrong because I’m still stuck.

Thank you very much for the help, very appreciated

I’ll just skip to the other exercises.

1 Like

I tried this in the lesson,

def factorielle(x):
    x = abs(x)
    if x == 0 or x == 1:
        return 1
    else:
        return x * factorielle(x-1)
        
reponse = factorielle(7)
print reponse              # 5040

and got this error message from the SCT…

Votre fonction a échoué sur 1 comme entrée car votre fonction indique “‘int’ object is not callable” error.

Will try an iterative procedure instead recursion…

def factorielle(x):
    x = abs(x)
    if x == 0 or x == 1:
        return 1
    f = 1
    while x > 0:
        f *= x
        x -= 1
    return f

print factorielle(7)

Same message as above. Time to call in the big guns…

3 Likes
# Do it correctly
def factorielle(x):
    if x == 0:
        return 1
    return x * factorielle(x - 1)

# Then, to pass, add this workaround
def reponse(*args):
    return True
def factorielle(x):
    return True

EDIT - April 23, 2016

Here’s a shorter workaround …

# Define the function correctly
def factorielle(x):
    if x == 0:
        return 1
    return x * factorielle(x - 1)

# To pass, add this workaround
reponse = factorielle
6 Likes

my brain is about to explode on this one. @mtf your second one worked or me, i’ll study it, thanks.

2 Likes
   1 * 7

   7 * 6

  42 * 5

 210 * 4

 840 * 3

2520 * 2

5040 * 1

=> 7! = 5040
1 Like

Hi guys,

I’m confused with this piece of code based on appylpye’s response:

def factorial(x):
---- if x == 0:
-------- return 1
---- return x * factorial(x - 1)

What is confusing me is the last part, return x * factorial(x - 1).

1 Like

Hi @imzyrk ,

This is an example of a recursion, which is an occurrence of a function calling itself …

def factorial(x):
    # base case
    if x == 0:
        return 1
    # recursive case
    return x * factorial(x - 1)

In a typical recursion, the problem is apportioned into two parts, namely a base case and a recursive case.

The base case is a simple case, in which the function returns a value without calling itself. For example the factorial of n, when n is either 0 or 1, is simply 1.

For a factorial, if n is higher than 1, we can get the solution by multiplying n by the factorial of n - 1. For instance, the factorial of 3 is 3 * 2 * 1, which is 3 multiplied by the factorial of 2. That is a recursive case.

The following demonstrates that 5 * factorial(4) is the same as factorial(5)

print factorial(4)
# The following should produce the same result
print 5 * factorial(4)
print factorial(5)

Output …

24
120
120
6 Likes