We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b16e235 commit 075a379Copy full SHA for 075a379
factorial.py
@@ -1,19 +1,13 @@
1
-#!/usr/bin/env python3
2
-#function computes factorial of a given number
3
def factorial(n):
4
- result = 1
5
- i=1
6
- while i<=n:
7
- result*=i
8
- i+=1
9
- return result
10
-def test_answer():
11
- assert factorial(4) == 24
12
-
13
-#read input from user
14
-n = 5 #int(input('Enter a number: '))
15
16
-#calculate factorial
17
-result = factorial(n)
18
19
-print(result)
+ if not isinstance(n, int):
+ raise TypeError("Input must be an integer")
+ if n < 0:
+ raise ValueError("Factorial is not defined for negative numbers")
+ if n > 999:
+ raise ValueError("Input too large - maximum value is 999")
+ if n == 0:
+ return 1
+ result = 1
+ for i in range(1, n + 1):
+ result *= i
+ return result
0 commit comments