+ 1
Why is this code not working??
def factorial (x): if x==1: return 1 elif x==0: return 1 else: y=x-1 z=x*factorial(y) return z a=int(input('factorial')) print(a) i created this to find factorial of a number. example: factorial of 4= 4x3x2x1 and also factorial of 1 and 0 is 1 only
5 Answers
+ 2
python is a great calculator! No need to write a case for 1 and 0.
your print is outside your function.
You are printing the word factorial and whatever the person inputs.
https://code.sololearn.com/c44ViIxaml84/?ref=app
Try and mod this to accept input its easy have fun!
+ 12
Change last line to -
print(factorial(a))
+ 7
Your code works fine, you just didn't call the function. Nikhil's suggestion fixes that.
Here is another short version
https://code.sololearn.com/c2x1vuNGnc1k/#py
+ 2
A little shorter function.
I don't love the multi-level If's.
https://code.sololearn.com/c9Lq5LrXB1S6/?ref=app
0
Thank you Everyone đ