1

I'm a beginner in Python. I'm following "Learn Python In The Hard Way". In Exercise 40, I tried to write a short code but got an error. Please help me :(

Source

class showInfo(object):
    'Initialize a classL'
    def __int__(self, name, phone, age):
        self.name = name
        self.phone = phone
        self.age = age

def showName(self):
    print("Name: "+self.name)
def showAge(self):
    print("Age: "+self.age)
def showPhone(self):
    print("Phone: "+self.phone)

emp1 = showInfo("JJJ")

emp1.showName()

Debug

Traceback (most recent call last):
File "classes.py", line 15, in <module>
    emp1 = showInfo("JJJ")
TypeError: object() takes no parameters

1 Answer 1

9

The cause is that __init__ is misspelled :-)

After that, there will be a different error message because showInfo("JJJ") only passes in one parameter when three are needed showInfo(somename, somephone, someage).

After that, there will be yet one more message because the last three methods are not properly indented under the class definition.

Here is the fixed-up code:

class showInfo(object):
    'Initialize a classL'
    def __init__(self, name, phone, age):
        self.name = name
        self.phone = phone
        self.age = age

    def showName(self):
        print("Name: "+self.name)

    def showAge(self):
        print("Age: "+self.age)

    def showPhone(self):
        print("Phone: "+self.phone)

emp1 = showInfo("Tom", "555-1212", 21)
emp1.showName()

This outputs:

Name: Tom
Sign up to request clarification or add additional context in comments.

1 Comment

Also, titlecase should be used for defining classname. class ShowInfo(object).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.