Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions myprogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Optional
import logging

class Person:
"""
Represent a person in python
"""
def __init__(self, firstname, lastname, age):
"""
Initialize a person with firstname, lastname and age
"""
self.firstname = firstname
self.lastname = lastname
self.age = age


def average_age(persons: Optional[list[Person]]) -> Optional[int]:
"""
Compute the average age of the list of persons
"""
if persons:
try:
all_ages = list(map(lambda p: p.age, persons))
return sum(all_ages) / len(all_ages)
except Exception:
logging.error("error happened when computing the average")
return None
return None
print("computing all ages done")

p1 = Person("John", "Doe", 51)
p2 = Person("Luke", "Skywalker", 21)
list_of_persons = [p1, p2]
print("Average age")
print(average_age(list_of_persons))