Can copy code under from YouTube link.
class Robot:
def __init__(self, name, color, weight):
self.name = name
self.color = color
self.weight = weight
def introduce_self(self):
print(f"My name is " + self.name)
# r1 = Robot()
# r1.name = "Tom"
# r1.color = "red"
# r1.weight = 30
#
# r2 = Robot()
# r2.name = "Jerry"
# r2.color = "blue"
# r2.weight = 40
r1 = Robot("Tom", "red", 30)
r2 = Robot("Jerry", "blue", 40)
r1.introduce_self()
r2.introduce_self()
Output:
My name is Tom
My name is Jerry
So it's working.
Code over do i think is part-2,as you see some code is comment out.
To use comment out code it would be like this,also trow i in
f-string
.
class Robot:
def introduce_self(self):
print("My name is {self.name}")
Use:
>>> r1 = Robot()
>>> r1.name = "Tom"
>>> r1.color = "red"
>>> r1.name
'Tom'
>>> r1.introduce_self()
My name is Tom
So here is manually making the
__init__
method in part 2.