0
Why is it not typing the whole print line entered in function battery? The only output is 75.why?
6 ответов
+ 2
class Car():
def __init__(self,name,battery=70):
self.name = name
self.battery = battery
def batteryLife(self):
print("It is "+ str(self.battery))
my_car = Car("corvette",75)
print(my_car.battery)
my_car.batteryLife()
1) You have given the instance variable and the method the same name "battery", it will conflict and return an error. So I have changed it.
2) Also you are just printing the instance variable battery, which returns 75.
3) To print what you want, you must call the method using the instance of the class. The last line I have added.
+ 2
Atul
my_car.battery refers to object property..
And to call function, mention with braces like my_car.battery()
(with argument if need any, and you should avoid name conflicts, you should use different name for methods and properties, like @avinesh given you full code with needed changes...)
+ 1
You are calling battery property value, not function..
And also,
Before calling, change function name to other name
0
Jayakrishna🇮🇳 can you tell me the exact code...
0
Avinesh thankyou very much...
0
Jayakrishna🇮🇳 thanks