1+
2+ #EXERCISE 1::
3+
4+ #Play computer with this code. Predict what you expect each line will do.
5+ #Then run the code and check your predictions. (If any lines cause errors, you may need to comment them out to check later lines).
6+
7+ #SOLUTION:
8+
9+ # The first four prints will work fine, as the Child class inherits from the Parent class and has access to its methods.
10+ # The last four prints will get error because the Parent class does not have the methods get_full_name and change_last_name defined.
11+ # Fix the code we can comment out the last four lines or we can add the methods to the Parent class.
12+
13+ class Parent :
14+ def __init__ (self , first_name : str , last_name : str ):
15+ self .first_name = first_name
16+ self .last_name = last_name
17+
18+ def get_name (self ) -> str :
19+ return f"{ self .first_name } { self .last_name } "
20+
21+
22+ class Child (Parent ):
23+ def __init__ (self , first_name : str , last_name : str ):
24+ super ().__init__ (first_name , last_name )
25+ self .previous_last_names = []
26+
27+ def change_last_name (self , last_name ) -> None :
28+ self .previous_last_names .append (self .last_name )
29+ self .last_name = last_name
30+
31+ def get_full_name (self ) -> str :
32+ suffix = ""
33+ if len (self .previous_last_names ) > 0 :
34+ suffix = f" (née { self .previous_last_names [0 ]} )"
35+ return f"{ self .first_name } { self .last_name } { suffix } "
36+
37+ person1 = Child ("Elizaveta" , "Alekseeva" )
38+ print (person1 .get_name ())
39+ print (person1 .get_full_name ())
40+ person1 .change_last_name ("Tyurina" )
41+ print (person1 .get_name ())
42+ print (person1 .get_full_name ())
43+
44+ """
45+ person2 = Parent("Elizaveta", "Alekseeva")
46+ print(person2.get_name())
47+ print(person2.get_full_name())
48+ person2.change_last_name("Tyurina")
49+ print(person2.get_name())
50+ print(person2.get_full_name())
51+ """
0 commit comments