Skip to content

Commit aa17b7a

Browse files
authored
Update calculate-factorial.md
1 parent 0aff8c7 commit aa17b7a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

snippets/python/math-and-numbers/calculate-factorial.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ tags: math,factorial,recursive-function
77

88
```py
99
def factorial(n):
10-
if type(n) != int or n < 0: raise ValueError("Invalid value of input: '" + str(n) + "'") # Raises an error for invalid input
11-
if n == 0 or n == 1: return 1 # Returns 1 if n is 0 or 1
10+
if n < 0: return "Exception: Cannot calculate factorial for negative numbers" # Handling negative number
11+
elif n == 0 or n == 1: return 1 # Returns 1 if n is 0 or 1
1212
else: return n * factorial(n-1) # Recall the factorial function
1313
```
1414

1515
# Usage:
1616
print(factorial(4)) # Returns 24
17-
print(factorial(-3)) # Returns type error for invalid inputs
17+
print(factorial(-3)) # Returns Exception

0 commit comments

Comments
 (0)