File tree Expand file tree Collapse file tree 1 file changed +11
-10
lines changed
Expand file tree Collapse file tree 1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change 77
88def validate_age (age ):
99 """Validate and process age input.
10-
10+
1111 This function validates that the provided age is a valid positive integer
1212 within a reasonable range (0-150 years).
13-
13+
1414 Args:
1515 age: The age input to validate (can be int, str, or float)
16-
16+
1717 Returns:
1818 int: The validated age as an integer
19-
19+
2020 Raises:
2121 ValueError: If age is invalid, negative, or out of range
2222 TypeError: If age cannot be converted to a number
23-
23+
2424 Examples:
2525 >>> validate_age(25)
2626 25
@@ -44,26 +44,27 @@ def validate_age(age):
4444 try :
4545 # Convert to float first to handle string numbers
4646 age_float = float (age )
47-
47+
4848 # Check if it's a whole number
4949 if age_float != int (age_float ):
5050 age_int = int (age_float )
5151 else :
5252 age_int = int (age_float )
53-
53+
5454 except (ValueError , TypeError ):
5555 raise ValueError ("Age must be a valid number" )
56-
56+
5757 # Validate range
5858 if age_int < 0 :
5959 raise ValueError ("Age must be a positive number" )
60-
60+
6161 if age_int > 150 :
6262 raise ValueError ("Age must be between 0 and 150" )
63-
63+
6464 return age_int
6565
6666
6767if __name__ == "__main__" :
6868 import doctest
69+
6970 doctest .testmod ()
You can’t perform that action at this time.
0 commit comments