-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex77.py
52 lines (38 loc) · 917 Bytes
/
ex77.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/home/wizard/anaconda3/bin/python
"""
Test factorial function
>>> factorial(5)
120
Test with negative value
>>> factorial(-5)
Traceback (most recent call last):
...
ValueError: n cannot be negative
Test with collection
>>> [factorial(x) for x in range(7)]
[1, 1, 2, ..., 120, 720]
"""
def factorial(n):
if n < 0:
raise ValueError('n cannot be negative')
if type(n) is not int:
raise TypeError('Type of n must be integer')
if n > 1:
return n * factorial(n-1)
return 1
def foo(n):
"""
Test with 6
>>> foo(6)
True
>>> foo(5)
False
"""
return (n % 2) == 0
if __name__ == '__main__':
import doctest
opts = doctest.DONT_ACCEPT_TRUE_FOR_1 \
| doctest.ELLIPSIS \
| doctest.NORMALIZE_WHITESPACE \
| doctest.IGNORE_EXCEPTION_DETAIL
doctest.testmod(verbose=True, optionflags=opts)