5. Factorial


#1


I'm having troubles with the factorial exercise:

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

It calculates the factorial correctly (a 24 for x=4, a 120 for x=5, etc...) but then I get this error:

Oops, try again. factorial(1) resulted in an error: invalid literal for int() with base 10: ''

Any idea on why this keeps happening? Thanks in advance.


def factorial(x):
    x=int(raw_input("Number?")) #Number
    f=x #Factorial 
    
    while x > 1:
      x=x-1
      f=f*x
    
    
    print "The factorial is" , f
    return f
    
factorial(x)


#2

don't overwrite the function parameter, if you want to prompt the user for a number, do this outside the function:

x=int(raw_input("Number?")) #Number
factorial(x)

and pass the variable as argument when calling the function


#3

It works! Thank you! :smiley:


#4