input: positive integer 'n' returns the factorial of 'n' (n!) >>> factorial(0) 1 >>> factorial(20) 2432902008176640000 >>> factorial(-1) Traceback (most recent call last): ... AssertionError: 'n' must been a int and >= 0 >>> factorial("test") Tra
(n)
| 765 | |
| 766 | |
| 767 | def factorial(n): |
| 768 | """ |
| 769 | input: positive integer 'n' |
| 770 | returns the factorial of 'n' (n!) |
| 771 | |
| 772 | >>> factorial(0) |
| 773 | 1 |
| 774 | >>> factorial(20) |
| 775 | 2432902008176640000 |
| 776 | >>> factorial(-1) |
| 777 | Traceback (most recent call last): |
| 778 | ... |
| 779 | AssertionError: 'n' must been a int and >= 0 |
| 780 | >>> factorial("test") |
| 781 | Traceback (most recent call last): |
| 782 | ... |
| 783 | AssertionError: 'n' must been a int and >= 0 |
| 784 | """ |
| 785 | |
| 786 | # precondition |
| 787 | assert isinstance(n, int) and (n >= 0), "'n' must been a int and >= 0" |
| 788 | |
| 789 | ans = 1 # this will be return. |
| 790 | |
| 791 | for factor in range(1, n + 1): |
| 792 | ans *= factor |
| 793 | |
| 794 | return ans |
| 795 | |
| 796 | |
| 797 | # ------------------------------------------------------------------- |
no outgoing calls
no test coverage detected