0

Is this correct implementation for - Create a Car class and as a user I want to use the car .So I need to get into the car

#python class Person:pass class Car: persons=[] def get_in(self,person:Person): self.persons.append(person)

16th Oct 2020, 2:32 PM
Deepak Jayaprakash
Deepak Jayaprakash - avatar
4 odpowiedzi
+ 1
Basically, except youre missing the init method. look at this code, I cant save it here right now class Person: def __init__(self,fname,lname): self.fname = fname self.lname = lname class Car: def __init__(self): self.passengers = [] def get_in(self, person): self.passengers.append(person) print(f"{person.fname} got in the car!") if __name__ == "__main__": billy = Person("Billy","Bob") sally = Person("Sally","Mae") car = Car() car.get_in(billy) print("Passengers:") for p in car.passengers: print(p.fname,p.lname) car.get_in(sally) print("Passengers now:") for p in car.passengers: print(p.fname,p.lname)
18th Oct 2020, 1:48 AM
Slick
Slick - avatar
0
Slick According to requirement it does not specify whether the person who wants to get in , is a passenger or driver correct me if I'm wrong
18th Oct 2020, 7:01 AM
Deepak Jayaprakash
Deepak Jayaprakash - avatar
0
That is correct
18th Oct 2020, 10:02 AM
Slick
Slick - avatar
0
I see right definition of a class here, but you ask me if the implementation is ok?
21st Oct 2020, 11:40 AM
Kaloyan Kostov
Kaloyan Kostov - avatar