Need Help with my class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need Help with my class

I'm trying to create a class which gets user input and I want to save each input as a variable. I did try something like the below but it all gets overrided. can someone suggest me how to I achieve this interactively MY CODE class Car: def __init__(self,name,color): self.name = name self.color = color while True: prompt = str(input("enter the name of car: ")) if prompt == 'q': break bmw = Car(prompt,"white") benz = Car(prompt,"black") print(bmw.name, bmw.color) print(benz.name, benz.color) now the problem is I want to give input and it should get stored respectively how can I achieve that. I don't know how to use oop effectively can some one guide me... Thanks in advance!

14th Jul 2016, 8:35 AM
sundar
sundar - avatar
5 Answers
+ 3
Create a class variable and assign your input to the variable when you pass it to the constructor. class Car: input = "" def __init__(self,name,color): self.input = name self.color = color while True: prompt = input("enter the name of car: ") if prompt == 'q': break bmw = Car(prompt,"white") benz = Car(prompt,"black") print (bmw.input) print (benz.input)
14th Jul 2016, 1:13 PM
Gershon Fosu
Gershon Fosu - avatar
+ 1
Oh totally missed your reply. I guess I can show you my use of classes in programs. If you're interested. gfosu1997@gmail.com
25th Jul 2016, 8:28 AM
Gershon Fosu
Gershon Fosu - avatar
0
may I see some example code
16th Jul 2016, 8:44 AM
sundar
sundar - avatar
0
can I be in your class?
25th Jul 2016, 3:14 AM
Brenden Jensen
Brenden Jensen - avatar
0
Mmm will mail u today evening having some queries too plz clarify me when u are free
25th Jul 2016, 8:34 AM
sundar
sundar - avatar