From 95a7eaf358853cdc3358bf80f854f465d6ff6dd9 Mon Sep 17 00:00:00 2001 From: NuttamasT Date: Wed, 23 Dec 2020 20:46:26 +0000 Subject: [PATCH] Remove global variables UCL-RITS#166 --- week09/refactoring/initial_global.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/week09/refactoring/initial_global.py b/week09/refactoring/initial_global.py index 21f1323..0a1c279 100644 --- a/week09/refactoring/initial_global.py +++ b/week09/refactoring/initial_global.py @@ -1,16 +1,16 @@ -def average_age(): +def average_age(group): """Compute the average age of the group's members.""" all_ages = [person["age"] for person in group.values()] return sum(all_ages) / len(group) -def forget(person1, person2): +def forget(group, person1, person2): """Remove the connection between two people.""" group[person1]["relations"].pop(person2, None) group[person2]["relations"].pop(person1, None) -def add_person(name, age, job, relations): +def add_person(group, name, age, job, relations): """Add a new person with the given characteristics to the group.""" new_person = { "age": age, @@ -50,12 +50,12 @@ def add_person(name, age, job, relations): "Zalika": "landlord" } -add_person("Nash", 34, "chef", nash_relations) +add_person(group, "Nash", 34, "chef", nash_relations) -forget("Nash", "John") +forget(group, "Nash", "John") if __name__ == "__main__": assert len(group) == 4, "Group should have 4 members" - assert average_age() == 28.75, "Average age of the group is incorrect!" + assert average_age(group) == 28.75, "Average age of the group is incorrect!" assert len(group["Nash"]["relations"]) == 1, "Nash should only have one relation" print("All assertions have passed!")