Factorial --> I don't know why?



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

it works like it supposed to

def factorial(x):
    x = int(raw_input("Enter a number: "))
    l = range(1, x+1)
    result = 1
    if x == 0:
        return result
    elif x > 0:
        for number in l: 
            result = result * number
        return result
        print("mathematically impossible !!")
print factorial(x)


x = int(raw_input("Enter a number: "))

No need to ask input from user. remove it.

the last part of code..
You don't need to print it,
even if you want to test the code , you should have given numerical value ...

print factorial(45)

reason for error..
When you pass x ,it has no value.
You also do not have to confuse with variable x inside factorial function.
Variables inside functions are only limited to that function and can't be used outside or by other function!


First of all , Thank you for replying , & thanks for the explanation
but , i don't see any problem if i ask input from user (in the instructions, he asked me to make the variable x as an input)

so, what should i do ?


Don't take user input and it should be fine.


Thanks Bro :wink:


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