-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathage_bands.py
More file actions
87 lines (71 loc) · 1.93 KB
/
age_bands.py
File metadata and controls
87 lines (71 loc) · 1.93 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import math
def age_band_5_years(age: int) -> str:
"""
Place age into appropriate 5 year band
This function takes the age supplied as an argument and returns a string
representing the relevant 5 year banding.
Parameters
----------
age : int
Age of the person
Returns
-------
out : str
The 5 year age band
Examples
--------
>>> age_band_5_years(3)
'0-4'
>>> age_band_5_years(None)
'Age not known'
>>> age_band_5_years(95)
'90 and over'
"""
if age is None:
return "Age not known"
if age >= 90:
if age >= 150:
raise ValueError("The age input: {} is too large.".format(age))
else:
return "90 and over"
elif age < 0:
raise ValueError("The age input: {} is too low.".format(age))
else:
lowerbound = 5 * int(math.floor(age / 5))
upperbound = lowerbound + 4
return "{}-{}".format(lowerbound, upperbound)
def age_band_10_years(age: int) -> str:
"""
Place age into appropriate 10 year band
This function takes the age supplied as an argument and returns a string
representing the relevant 10 year banding.
Parameters
----------
age : int
Age of the person
Returns
-------
out : str
The 10 year age band
Examples
--------
>>> age_band_10_years(3)
'0-9'
>>> age_band_10_years(None)
'Age not known'
>>> age_band_10_years(95)
'90 and over'
"""
if age is None:
return "Age not known"
if age >= 90:
if age >= 150:
raise ValueError("The age input: {} is too large.".format(age))
else:
return "90 and over"
elif age < 0:
raise ValueError("The age input: {} is too low.".format(age))
else:
lowerbound = 10 * int(math.floor(age / 10))
upperbound = lowerbound + 9
return "{}-{}".format(lowerbound, upperbound)