Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Last active December 23, 2018 08:22
Show Gist options
  • Save anmolj7/04567e9ddc31386d418985917f08258a to your computer and use it in GitHub Desktop.
Save anmolj7/04567e9ddc31386d418985917f08258a to your computer and use it in GitHub Desktop.
#Not using float because the we're multiplying not dividng, in multiplying, the end result will always be a whole number.
def factorial(number):
return 1 if number < 2 else number * factorial(number-1)
#Used number < 2 instead of number == 1 or number == 0, It's shorter.
try:
number = int(input("Enter A Number: ")) #Converting To Int cause we can find factorial
#of only integer and not decimal values ..
except ValueError:
#If the user types some other type of data, for example
#He enters string, we obviously can't find the factorial of string,
print("Please Enter A Valid Input.")
exit()
factorial(number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment