0

I am learning about classes and objects in Python, but when I run this code the output is an error. Can anyone show me how to fix it?

class Person :
def _init_ (self,name,age) :
    self.name = name
    self.age = age


def myfunction(self) :
        print("Hello My Name Is" + self.name)

 p1 = Person("John",36)
 p1.myfunction()

Here's the error output:

Traceback (most recent call last): File "F:\IPB\PENUGASAN\SEMESTER 2\STRAKDAT\Penugasan\Pertemuan 3\Materi 3.py", line 16, in p1 = Person("John",36) TypeError: Person() takes no arguments

------------------ (program exited with code: 1)

Press any key to continue . . . Terminate batch job (Y/N)?

ad absurdum
  • 19,498
  • 5
  • 37
  • 60

2 Answers2

1

Two things:
Your indentation is not set properly.
The __init__ should be with double underscores.

Try this:

class Person:
    def __init__(self,name,age) :
        self.name = name
        self.age = age


    def myfunction(self) :
        print("Hello My Name Is " + self.name)


p1 = Person("John",36)
p1.myfunction()
anjandash
  • 797
  • 10
  • 21
0

Your _init_ function in your class is not the main initializer. To make it that, rename it to __init__.

llamaking136
  • 421
  • 5
  • 16