Skip to content

Commit ecd74be

Browse files
committed
improve the readability of the duct output by adding indentation for desentant objects
1 parent ea186be commit ecd74be

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

prep-exercises/exercise_nine.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ class Person:
88
age: int
99
children: List["Person"]
1010

11-
#Add a grandchild to the main object imran
12-
khaled = Person(name="Khaled", age=6, children=[])
11+
#Add great grandchildren to the main object imran to demonsrate that function works recursively
12+
jameeela = Person(name="Jameeela", age=10, children=[])
13+
sameera = Person(name="Sameera", age=8, children=[])
14+
15+
#Add a grandchild to the main object imran to demonsrate that function works recursively
16+
khaled = Person(name="Khaled", age=32, children=[jameeela, sameera])
1317

1418
#Add "age" field and thier respective values to "fatma" and "aisha" - instances of class "Person"
15-
fatma = Person(name="Fatma", age=14, children=[])
16-
aisha = Person(name="Aisha",age=22, children=[khaled])
19+
fatima = Person(name="Fatma", age=46, children=[])
20+
aisha = Person(name="Aisha",age=53, children=[khaled])
1721

1822
##Add "age" field and its values to "imran" - instance of class "Person"
19-
imran = Person(name="Imran", age=44, children=[fatma, aisha])
23+
imran = Person(name="Imran", age=79, children=[fatima, aisha])
2024

21-
def print_family_tree(person: Person) -> None:
22-
print(person.name)
25+
def print_family_tree(person: Person, level: int = 0) -> None:
26+
indent = " " * level
27+
print(f"{indent}{person.name} ({person.age})")
2328
for child in person.children:
24-
print(f"- {child.name} ({child.age})")
25-
print_family_tree(child)
29+
print_family_tree(child, level + 1)
2630

2731
print_family_tree(imran)
2832

0 commit comments

Comments
 (0)