Why is this code not working?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

18th Aug 2018, 2:04 AM
Anaadi Dubey
Anaadi Dubey - avatar
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!
18th Aug 2018, 2:34 AM
Mike Utty
Mike Utty - avatar
+ 12
Change last line to - print(factorial(a))
18th Aug 2018, 2:40 AM
Nikhil
Nikhil - avatar
+ 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
18th Aug 2018, 2:51 AM
David Ashton
David Ashton - avatar
+ 2
A little shorter function. I don't love the multi-level If's. https://code.sololearn.com/c9Lq5LrXB1S6/?ref=app
18th Aug 2018, 3:12 AM
Amir Galanty
Amir Galanty - avatar
0
Thank you Everyone 😊
18th Aug 2018, 4:53 PM
Anaadi Dubey
Anaadi Dubey - avatar