Factorials - UnboundLocalError: local variable 'answer' referenced before as*ignment


#1


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

UnboundLocalError: local variable 'answer' referenced before as*ignment

Am I using an incorrect variable or is my formatting wrong?


Replace this line with your code. 
def factorials(x):
    if x > 1:
        answer = x * factorial(x-1) 
        print answer
    elif x == 0 :
        answer = 1
    else:
        print 'Enter positive integer'
    return answer
    

print factorials(1)


#2

Because there is an error in the line above this one, answer is still not defined.

The fix is to rename your function to, factorial.

There may be other issues, so ping us if you are still having problems.


#3

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