File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ #EXERCISE 1: Fix the above code so that it works. You must not change the print on line 17
3+ # we do want to print the children’s ages. (Feel free to invent the ages of Imran’s children)
4+
5+ # SOLUTION:
6+
7+ from dataclasses import dataclass
8+ from typing import List
9+
10+ @dataclass (frozen = True )
11+ class Person :
12+ name : str
13+ age : int = 0
14+ children : List ["Person" ]
15+
16+ fatma = Person (name = "Fatma" , age = 18 , children = [])
17+ aisha = Person (name = "Aisha" , age = 24 , children = [])
18+
19+ imran = Person (name = "Imran" , age = 45 , children = [fatma aisha ])
20+
21+ def print_family_tree (person : Person ) -> None :
22+ print (person .name )
23+ for child in person .children :
24+ print (f"- { child .name } ({ child .age } )" )
25+
26+ print_family_tree (imran )
You can’t perform that action at this time.
0 commit comments