+ 1

It tells robot takes no argument in below code can anybody tell me what's wrong in the code?

class Robot: def _init_ (self, name, color, weight): self.name = name self.color = color self.weight = weight def introduce_self(self): print("My name is " + self.name) r1 = Robot("Tom", "red", 40) r2 = Robot("Jerry", "blue", 30) r1.introduce_self() r2.introduce_self()

6th Aug 2020, 7:47 PM
Ashish Singh
5 odpowiedzi
0
The error is that Robot() takes no argument, but why? It is because the constructor function is a special method. In Python, special methods are written with two underscores before and after. It is __init__ not _init_ Check it in the code play ground.
6th Aug 2020, 7:57 PM
Ali Abdelhady
Ali Abdelhady - avatar
0
Check the code again _init_ is already written.
6th Aug 2020, 8:03 PM
Ashish Singh
0
Ashish Singh Check my comment again, it is __init__ not _init_
6th Aug 2020, 8:05 PM
Ali Abdelhady
Ali Abdelhady - avatar
0
Again same error
6th Aug 2020, 8:14 PM
Ashish Singh
0
It works in here The only modification made is the one mentioned above: "__init__" https://code.sololearn.com/cd060nE1urUc/?ref=app
6th Aug 2020, 8:19 PM
Ali Abdelhady
Ali Abdelhady - avatar