factorial.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. #Program to find factorial of certain number
  2. print("This code was developed by @PythonCoder8 on Github. To see @PythonCoder8's profile go to github.com/PythonCoder8/.")
  3. #Import sys module for verifying user input
  4. import sys
  5. #Ask for user input to enter a number
  6. userInput = input("Enter a number: ")
  7. #If user's input is of float type.....
  8. if isinstance(userInput, float) == True:
  9. #Exit program with the message "Decimals are not allowed. Only whole numbers are."
  10. sys.exit("Decimals are not allowed. Only whole numbers are.")
  11. #If error occurs when converting the input to int type, exit program with the message "Your input is not a number."
  12. try:
  13. userNumber = int(userInput)
  14. except:
  15. sys.exit("Your input is not a number. Exiting Program.")
  16. #Create a variable named "userProduct", and assign a value of 0
  17. userProduct=1
  18. #Create for loop ranging from 1 to userNumber (variable declared above) + 1, and inside the for loop, ...
  19. for i in range(1,userNumber+1):
  20. #Keep on adding loop variable to "userProduct"
  21. userProduct *= i
  22. #Display message showing factorial of number user entered
  23. print("The factorial of %s (!%s) is %d" %(userInput, userInput, userProduct))