title: Python: controlling input to a factorial method
body: <p>I want this python function to handle the user input not to provide a non-integer value (decimals for instance) and through an exception otherwise.</p>
  
  <pre><code>    # a function that takes an integer and returns its factorial
  def factorial(x):
      try:
          if x == 0:
              return (0)
          else:
              pro = 1
              for i in range(1, x + 1):
                  pro *= i
      except(ValueError, TypeError):
          # this part of the code is supposed to handle exception for invalid input5
          print("an error happened")
      return (pro)
  
  
  # calling the function with validated input
  # warning!!! this program my take longer time as your input get larger
  num = int(input("Enter a positive integer:"))
  if num &gt;= 0:
      print("\n Factorial of " + str(num) + " = " + str(factorial(num)))
  else:
      print("Factorial is operated only on positive number");
  </code></pre>