0

How to have use multiple user input using OOP

This is my code, https://code.sololearn.com/cvOk8GfNHiHJ/#py. Im trying to make it so you can have the user input the name and age of each person, d1 mean dads name, da1, means dads age and so on. I keep getting this error: TypeError: __init__() missing 36 required positional arguments: 'd1', 'da1', 'm1', 'ma1', 'b1', 'ba1', 'b2', 'ba2', 'b3', 'ba3', 'b4', 'ba4', 'b5', 'ba5', 'b6', 'ba6', 's1', 'sa1', 's2', 'sa2', 's3', 'sa3', 's4', 'sa4', 's5', 'sa5', 's6', 'sa6', 'g1', 'ga1', 'g2', 'ga2', 'grm1', 'grma1', 'grm2', and 'grma2'. Any help would be awesome!!

27th Dec 2017, 6:52 PM
Ole113
Ole113 - avatar
1 Resposta
0
Sorry for late answer but I think I came up with a decent solution to your problem, but before that you are getting that error because you need to assign a value to each of the parameters in the __init__ unless you use default values, for example: class Car: def __init__(self, brand, color, top_speed): <some code> WRONG: my_car = Car("Audi", "Blue") # top_speed value is not given CORRECT: my_car = Car("Audi", "Green", "120mph") Also in your code I seen you use "print("")" to new lines, the print function already adds a new line before printing out the value but if you need to add more in the future do \n e.g print("Hello\nWorld\n\n!") Code that works and I tested: class Person: """ Base family member class """ # this gets executed when the object gets created def __init__(self, family_member_title, name, age): self.fam_mem = family_member_title # mom, dad, uncle ect self.name = name self.age = age def show(self): """ Prints the family member's information """ print(self.fam_mem.upper() + ":") print("Name: {}".format(self.name)) print("Age: {} \n".format(self.age)) class Family: def show(self): player.show() dad.show() player = Person("You", str(input("Your name: ")), str(input("Your age: "))) dad = Person("Dad", str(input("Your dad's name: ")), str(input("Your dad's age: "))) # add as many as you want remember to add .show() in family class family = Family() family.show() I think this is good because you can add more functionality if needed to either classes. I hope this helps and if you got more questions don't be afraid to ask.
12th Jan 2018, 10:53 PM
TurtleShell
TurtleShell - avatar