1+ """ ✍️exercise
2+ Play computer with this code. Predict what you expect each line will do. Then run the code and check your predictions. (If any lines cause errors, you may need to comment them out to check later lines).
3+ """
4+
5+ # PREDICTION: This defines a base class called Parent
6+ class Parent :
7+ # PREDICTION: Constructor that sets first_name and last_name attributes
8+ def __init__ (self , first_name : str , last_name : str ):
9+ self .first_name = first_name
10+ self .last_name = last_name
11+
12+ # PREDICTION: Method that returns full name as "first last"
13+ def get_name (self ) -> str :
14+ return f"{ self .first_name } { self .last_name } "
15+
16+
17+ # PREDICTION: Child class inherits everything from Parent class
18+ class Child (Parent ):
19+ # PREDICTION: Constructor calls parent's constructor then adds new attribute
20+ def __init__ (self , first_name : str , last_name : str ):
21+ super ().__init__ (first_name , last_name ) # PREDICTION: Calls Parent.__init__
22+ self .previous_last_names = [] # PREDICTION: Creates empty list for this instance
23+
24+ # PREDICTION: Method to change last name and track previous names
25+ def change_last_name (self , last_name ) -> None :
26+ self .previous_last_names .append (self .last_name )
27+ self .last_name = last_name
28+
29+ # PREDICTION: Method that returns full name with maiden name note if changed
30+ def get_full_name (self ) -> str :
31+ suffix = ""
32+ if len (self .previous_last_names ) > 0 :
33+ suffix = f" (née { self .previous_last_names [0 ]} )"
34+ return f"{ self .first_name } { self .last_name } { suffix } "
35+
36+
37+ # PREDICTION: Creates Child instance with names "Elizaveta" "Alekseeva"
38+ person1 = Child ("Elizaveta" , "Alekseeva" )
39+ # PREDICTION: Prints "Elizaveta Alekseeva" (calls inherited get_name() from Parent)
40+ print (person1 .get_name ())
41+ # PREDICTION: Prints "Elizaveta Alekseeva" (no suffix since no name change yet)
42+ print (person1 .get_full_name ())
43+ # PREDICTION: Changes last name to "Tyurina", adds "Alekseeva" to previous_last_names
44+ person1 .change_last_name ("Tyurina" )
45+ # PREDICTION: Prints "Elizaveta Tyurina" (updated last name)
46+ print (person1 .get_name ())
47+ # PREDICTION: Prints "Elizaveta Tyurina (née Alekseeva)" (shows maiden name)
48+ print (person1 .get_full_name ())
49+
50+
51+ # PREDICTION: Creates Parent instance (NOT Child) with same names
52+ person2 = Parent ("Elizaveta" , "Alekseeva" )
53+ # PREDICTION: Prints "Elizaveta Alekseeva" (Parent has get_name() method)
54+ print (person2 .get_name ())
55+ # PREDICTION: ERROR! Parent class doesn't have get_full_name() method
56+ print (person2 .get_full_name ())
57+ # PREDICTION: ERROR! Parent class doesn't have change_last_name() method
58+ person2 .change_last_name ("Tyurina" )
59+ # PREDICTION: Won't reach this line due to previous error
60+ print (person2 .get_name ())
61+ # PREDICTION: Won't reach this line due to previous error
62+ print (person2 .get_full_name ())
0 commit comments