Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (16.4k points)


Figure and print the factorial of a given positive whole number. The Integer can be just about as extensive as 100. 

My effort:

I have given solutions a shot at different compilers, they are turned out great on different compilers, yet on hackerrank, it's not working which says compile-time error

# Enter your code here. Read input from STDIN. Print output to STDOUT

def fac(n):

    return 1 if (n < 1) else n * fac(n-1)

no = int(raw_input())

print fac(no)

Anyone, please help me.

1 Answer

0 votes
by (26.4k points)

This solution turns out only great for Python 2 - I executed your code on Hackerrank and it breezed through all the test cases. 

Along these lines, the compilation error appears if the code is gathered with Python 3

no = int(raw_input())

NameError: name 'raw_input' is not defined

That is genuine on the grounds that raw_input should be replaced with a input() in Python 3

In the event that the code with the amendment is executed subsequently, there's another issue:

print fac(no)


SyntaxError: invalid syntax

Once more, simply add brackets around fac(no), and afterward, the code accumulates and breezes through all the tests: 

In this way, the full code is underneath:

def fac(n):

    return 1 if (n < 1) else n * fac(n-1)

no = int(input())

print (fac(no))

Are you interested to learn the concepts of Python? Join the python training course fast!

Browse Categories