Why is it not typing the whole print line entered in function battery? The only output is 75.why? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Why is it not typing the whole print line entered in function battery? The only output is 75.why?

https://code.sololearn.com/coba8ilVAV7R/?ref=app

2nd Jul 2020, 3:21 PM
Atul
Atul - avatar
6 Antworten
+ 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.
2nd Jul 2020, 4:34 PM
Avinesh
Avinesh - avatar
+ 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...)
2nd Jul 2020, 5:49 PM
Jayakrishna 🇮🇳
+ 1
You are calling battery property value, not function.. And also, Before calling, change function name to other name
2nd Jul 2020, 3:38 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 can you tell me the exact code...
2nd Jul 2020, 4:00 PM
Atul
Atul - avatar
0
Avinesh thankyou very much...
3rd Jul 2020, 4:38 AM
Atul
Atul - avatar
3rd Jul 2020, 4:38 AM
Atul
Atul - avatar