Dec-06-2017, 08:15 PM
At line 19, I think I understand the logic of the first half: The method, __str__, returns a line break for each variable inside the template list, while also invoking the join function. Question #1: Is that correct or almost correct?
The second half of line 19 throws me off. How does the format function multiply the class variables exponentially (with ‘**’)? This question I’ve asked is sort of my feeble attempt at explaining it as best I can. Question #2: Can someone please explain the logic of the second half of line 19 of Mekire’s improved script?
Line 23 of Mekire’s script assigns the instances of all the class variables for the PassPort class. Question #3: If I wanted to now prompt the user in in my shell to ask for each variable, for example asking for the user’s first name to input into the first class variable instance, how do I do that? Here is my new script where I attempt to implement that feature (I am referring to line 23) :
Thanks for your attention.
The second half of line 19 throws me off. How does the format function multiply the class variables exponentially (with ‘**’)? This question I’ve asked is sort of my feeble attempt at explaining it as best I can. Question #2: Can someone please explain the logic of the second half of line 19 of Mekire’s improved script?
Line 23 of Mekire’s script assigns the instances of all the class variables for the PassPort class. Question #3: If I wanted to now prompt the user in in my shell to ask for each variable, for example asking for the user’s first name to input into the first class variable instance, how do I do that? Here is my new script where I attempt to implement that feature (I am referring to line 23) :
class PassPort(object): def __init__(self, first_name, middle_name, last_name, gender, YOB, iQ): self.species = 'homo-sapien' self.first_name = first_name self.middle_name = middle_name self.last_name = last_name self.gender = gender self.YOB = YOB # (Year of birth) self.iQ = iQ # (Intelligence Quotient) def __str__(self): template = ["The citizen's first name is {first_name}.", "The citizen's middle name is {middle_name}.", "The citizen's last name is {last_name}.", "The species of the citizen is {species}.", "The gender of the citizen is {gender}.", "The citizen was born in the year {YOB}.", "And finally, the citizen has an iQ of {iQ}."] return "\n".join(template).format(**vars(self)) def main(): # citizen256032 = PassPort('Jon', 'Ross', 'Dobbs', 'male', 1947, 44) citizen256032 = PassPort(first_name=input("What is your first name?"),'Ross', 'Dobbs', 'male', 1947, 44) print(citizen256032) if __name__ == "__main__": main()In my initial script I attempted to assess the iQ variable using some methods. Question #4: If I wanted to print a note below the iQ class instance under the __str__ method to indicate the status of the client's iQ (like if it is average, below average or genius), how could I do that better (than my attempt in my original post)?
Thanks for your attention.