-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEstimateAge.py
54 lines (41 loc) · 1.34 KB
/
EstimateAge.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
53
54
import requests
class AgifyAPI:
"""Class for working with the Agify.io API."""
def __init__(self, name):
"""
Initializes an API instance with the given name.
Args:
name: The name to estimate the age for.
"""
self.name = name
self.url = f"https://api.agify.io?name={name}"
def get_estimated_age(self):
"""
Retrieves the estimated age from the API.
Returns:
The estimated age as an integer, or None if an error occurs.
"""
try:
response = requests.get(self.url)
response.raise_for_status() # Raise an exception for non-200 status codes
data = response.json()
return int(data.get("age")) # Return age as an integer
except requests.exceptions.RequestException as e:
print(f"Error: An error occurred while making the API request: {e}")
except ValueError as e:
print(f"Error: Invalid JSON response received: {e}")
def main():
"""Main function of the program."""
# Get the name from the user
name = input("Enter name: ")
# Create an API instance
api = AgifyAPI(name)
# Get the estimated age from the API
estimated_age = api.get_estimated_age()
# Print the results
if estimated_age is not None:
print(f"Estimated age for {name}: {estimated_age}")
else:
print("Could not get estimated age.")
if __name__ == "__main__":
main()