Python Forum

Full Version: Class takes no arguments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey,

I am writing a basic class and I am having problems inserting arguments into the class "Dog".

Here is the source code

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

    def sit(self):
        print(self.name.title() + ' is now sitting.')

    def roll_over(self):
        print(self.name.title() + ' is now rolling over.')


my_dog = Dog('Willie', 6)
print("my dog's name is " + my_dog.name.title() + ".")
print('my dog is ' + str(my_dog.age) + '.')
here is the error message:
Error:
Traceback (most recent call last): File "C:/Users/maxya/PycharmProjects/classes.py", line 28, in <module> my_dog = Dog('Willie', 6) TypeError: Dog() takes no arguments
Thank you for your help.
The dunder init method is double underscore on each side __init__
Ok thank you so much!
(Jul-10-2019, 11:18 PM)Myang123 Wrote: [ -> ]Ok thank you so much!

Hello Myang123,

I saw this same problem and one question I had was already answered, but I had another that I hope you can help with.

How did you get the line below to work?

my_dog = Dog("Peso")

I placed it outside the code block(>>>) and inside the code block(...) and both failed.

The return message is(...) invalid syntax and (>>>) Dog is not defined.

I am hoping that you are able to point me in the right direction.

Thanks!
DJXIV