From 57a438d8f2363d9849e683c94586cfb44c8ba1ba Mon Sep 17 00:00:00 2001 From: Julien Date: Sat, 22 Jan 2022 17:19:29 -0700 Subject: [PATCH 1/2] Create myprogram.py --- myprogram.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 myprogram.py diff --git a/myprogram.py b/myprogram.py new file mode 100644 index 0000000..598131c --- /dev/null +++ b/myprogram.py @@ -0,0 +1,34 @@ +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(list_of_persons)) From b8ad94bdd045d6ba59e4b172c26dc40d20c85230 Mon Sep 17 00:00:00 2001 From: Julien Date: Sat, 22 Jan 2022 17:21:07 -0700 Subject: [PATCH 2/2] Update myprogram.py --- myprogram.py | 1 + 1 file changed, 1 insertion(+) diff --git a/myprogram.py b/myprogram.py index 598131c..132da9c 100644 --- a/myprogram.py +++ b/myprogram.py @@ -31,4 +31,5 @@ def average_age(persons: Optional[list[Person]]) -> Optional[int]: p1 = Person("John", "Doe", 51) p2 = Person("Luke", "Skywalker", 21) list_of_persons = [p1, p2] +print("Average age") print(average_age(list_of_persons))