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
1 Answer 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
Max Uppenkamp
9744 silver badges16 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
'_init_' != '__init__'sum = sum + Dog.agetosum += dog.age. When you callDog.ageyou will get anAttributeErrorbecause the classDogdoesn't have the attributeageset, the instance of the class has.