First page Back Continue Last page Overview Graphics

Robot Class with __init__

class Robot:

def __init__(self, name=None):

self.name = name

def say_hi(self):

if self.name:

print("Hi, I am " + self.name)

else:

print("Hi, I am a robot without a name")

x = Robot()

x.say_hi()

y = Robot("Marvin")

y.say_hi()

Hi, I am a robot without a name

Hi, I am Marvin

Output