Browse Source

Add files via upload

PythonCoder8 4 years ago
parent
commit
1b545baa46
1 changed files with 33 additions and 0 deletions
  1. 33 0
      factorial.py

+ 33 - 0
factorial.py

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