In: Computer Science
For python...
class car:
def __init__(self)
self.tire
self.gas
def truck(self,color)
style = color
return
def plane(self,oil)
liquid = self.oil + self.truck(color)
For the plane method, I am getting an error that the class does not define __add__ inside init so I cannot use the + operator. How do I go about this.
The error you were getting because self.truck(color) returns Empty object and you need to define how you want to addd objects to support the "+" operation. The correct code intuitievly would be
Code-
class car:
#initialized the new car with tire and gas
values
def __init__(self):
self.tire=0
self.gas=0
#Assign color to the object in style attribute
def truck(self,color):
self.style = color
return self.style
#update gas attribute of object using the plane
method
def plane(self,oil):
liquid = self.gas+oil
self.gas=liquid
return liquid
x=car()
print("Initially gas= ",x.gas," tire= ",x.tire)
x.truck("yellow")
print("Executing truck method gas= ",x.gas," tire= ",x.tire,"
style= ",x.style)
x.plane(100)
print("Executing plane method gas= ",x.gas," tire= ",x.tire,"
style= ",x.style)
Code Screenshots-
Outputs-
Feel free to comment for any issues, do rate the answer positively