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!
5 Respostas
+ 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)
+ 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
0
may I see some example code
0
can I be in your class?
0
Mmm will mail u today evening having some queries too plz clarify me when u are free