Factorial --> I don't know why?


#1



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


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
    else: 
        print("mathematically impossible !!")
        
print factorial(x)


#2

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 ...
like..

print factorial(45)

Note:
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!


#3

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 ?


#4

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


#5

Alright,
Thanks Bro :wink:


#6

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