How to make a method inside a class takes input

in the Jedi example :slight_smile:


class Jedi:
    def __init__(self, name):
        self.jedi_name = name

    def say_hi(self):
        print('Hello, my name is ', self.jedi_name)

j1 = Jedi('ObiWan')
j1.say_hi()

how can i make the say_hi take input , or take the name itself ? and why can’t I use name with it instead of self.jedi_name since self.jedi_name= name and they are both in the same class ?

Hi @sumerfat,

You can add parameters/arguments to any method.

For say_hi, for example, the self keyword is already an argument.

If you check the __init__ method, for example, in addition to self that is required for all methods, you have also the parameter called name.

To receive the name directly inside say_hi, you can just do the following:

def say_hi(self, my_parameter):
   print("Hello, ", my_parameter)

And call it with

j1 = Jedi('ObiWan')

j1.say_hi('MyObi-LAN')

You can add as many parameters as you want to a method:

def say_hi(self, first, second, third, fourth):
   print(first, second, third, fourth)

Please let us know if you have any further questions.

1 Like