0

I have just started learning to program in Python. I am finding it difficult to instance objects properly. It gives a traceback but I don't know why. I have been reading the documentation for 3.8 and still not sure what is causing the error? Your help is deeply appreciated.

Thank you.

 def _init_(self, name, age, character):
 self.name = name
 self.age = age
 self.character = character
 def date_of_birth():
 return 2020 - self.age
Breeds = Breeds = [Dog("Alsation", 2,["Protective","Smart"]), Dog("Rotteweiler", 3,["Possessive","Aggressive"]), Dog("Chihuahua",1,["Loud, Jumpy"])]
sum = 0
for dog in Breeds:
 sum = sum + Dog.age
print("The average age of breeds is: " + str(sum/len(Breeds)))'''
```Traceback (most recent call last):
 File "C:\Users\Hilary\Desktop\hello.py", line 12, in <module>
 Breeds = Breeds = [Dog("Alsation", 2,["Protective","Smart"]), Dog("Rotteweiler", 3,["Possessive","Aggressive"]), Dog("Chihuahua",1,["Loud, Jumpy"])]
TypeError: Dog() takes no arguments
>>> ```
Ferran Buireu
29.4k7 gold badges48 silver badges73 bronze badges
asked May 8, 2020 at 7:36
6
  • Dog() takes no arguments This says that Dog Class doesn't accept any arguments. Commented May 8, 2020 at 7:40
  • Please search how to create objects with parameters in python. Secondly Breeds = [Dog.. This assignment should be only once. You are done assignment twice. Commented May 8, 2020 at 7:41
  • 2
    '_init_' != '__init__' Commented May 8, 2020 at 7:45
  • Not related to your original question, but your loop to sum all the dogs age should be changed from: sum = sum + Dog.age to sum += dog.age. When you call Dog.age you will get an AttributeError because the class Dog doesn't have the attribute age set, the instance of the class has. Commented May 8, 2020 at 7:47
  • @HampusLarsson when I do that it gives a different answer. 0.3333333 instead of 2.0. Commented May 8, 2020 at 8:27

1 Answer 1

1

The __init__ method requires 2 underscores left and right, you are using 1.

Therefore your init method isn't recognized, and Python doesn't see any constructors that take your number of arguments.

answered May 8, 2020 at 7:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.