0

I want to Inheritance my animal class to Dog class.Everything is good until ı want to write Dog.add_info() at operation 1.I wrote Dog = Dog(animal) at line 46 but ı think there are a problem but ı can't find out what it is.I learning 'class' thing and 'Inheritance' thing first.

import random
class animal():

    def __init__(self,name,legs,place,move,weight,lenght):
        self.name = name
        self.legs = legs
        self.place = place
        self.move = move
        self.weight = weight
        self.lenght = lenght

    def __str__(self):
        return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nLenght: {}".format(self,self.name,self.legs,self.place,self.move,self.weight,self.lenght)
    def add_info(self):
        info_name = input("Enter name")
        info_legs = input("Enter how many legs this animal have")
        info_place = input("Enter where this animal live")
        info_move = input("Enter how move this animal")
        info_weight = input("Enter Weight")
        info_lenght =input("Enter lenght")
        self.name = info_name
        self.legs = info_legs
        self.place = info_place
        self.move = info_move
        self.weight = info_weight
        self.lenght = info_lenght

class Dog(animal):

    def __init__(self,name,legs,place,move,weight,lenght,feather,aggresivity):
        super().__init__(self,name,legs,place,move,weight,lenght)
        self.feather = feather
        self.aggresivity = aggresivity
    def __str__(self):
        return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nLenght: {}\nFeather: {}\nAggresivity {}".format(self, self.name, self.legs,self.place, self.move,self.weight, self.lenght,self.feather,self.aggresivity)
    def add_info(self):
        super().add_info()
        info_feather = input("Enter are your dog have feather or not 'have' or 'haven't")
        info_aggresivity =input("Enter are your dog aggresive or passive")
        self.feather = info_feather
        self.aggresivity = info_aggresivity
    def pick_random(self):
        list_dog = ["Labrador","Bulldog","Retriever,","Poodle","Beagle","Husky"]
        random_dog = random.choice(list_dog)
        print("Your dog is :",random_dog)
Dog = Dog(animal)
print("""
1 for add info to your dog
2 for get infos your dog have
3 for pick random dog type
q for quit
""")
choice = input("Enter operation: ")
while True:
    if (choice =="q"):
        print("BYE...")
        break
    elif(choice == "1"):
        Dog.add_info()

    elif(choice =="2"):
        pass

    elif(choice =="3"):
        pass

    else:
        print("İnvalid operation")
khelwood
  • 55,782
  • 14
  • 81
  • 108
mesut.a
  • 15
  • 5
  • 1
    You made the class `Dog` require 7 arguments: name, legs, place, move, weight, length, feather, and aggresivity. When you create an instance of the class in the line `Dog = Dog(animal)`, you only passed 1 argument. You need to do something like this: `dog = Dog("sparky", 4, "right here", True, "45 pounds", "30 inches", "friendly")`. Notice how `dog` is lower case: this is so that you don't replace the class when you create the variable. – Sylvester Kruin Sep 08 '21 at 23:19
  • But ı want add info with my function as is add_info how can ı get it – mesut.a Sep 08 '21 at 23:23
  • You can change the parameters of `__init__` to keyword arguments like `def __init__(self, name=None, legs=None, place=None, move=None, weight=None, lenght=None):`. When you omit these values, they would be `None`. – Sven Eberth Sep 08 '21 at 23:24
  • If you don't want to have to pass all those arguments when you create the `dog` instance, you can create defaults that will be used if the user does not pass those arguments: `def __init__(self, name="sparky", legs=4, place="right here", move=True, weight="45 pounds", length="30 inches", aggresivity="friendly"):` That way, if you don't set the `name` parameter, for example, when you create the instace of `Dog`, it assumes the name is `"sparky"`. – Sylvester Kruin Sep 08 '21 at 23:27
  • Another tip: you may want to put the line `choice = input(...` *inside* the `while` loop. This way, it will repeatedly ask you for your input until you hit quit, which I'm assuming is what you want. – Sylvester Kruin Sep 08 '21 at 23:30

3 Answers3

0

Dog expects all the same arguments as animal; you are passing the class animal itself as a single argument.

Rather than duplicating all the arguments from Animal.__init__, though, use keyword arguments to simplify the definition of Dog.__init__.

First, we'll clean up Animal a little. Note that you were passing self unnecessarily to a lot of methods, as super() already captures the value to pass.

class Animal:

    def __init__(self, *, name, legs, place, move, weight, length, **kwargs):
        super().__init__(**kwargs)
        self.name = name
        self.legs = legs
        self.place = place
        self.move = move
        self.weight = weight
        self.length = length

    def __str__(self):
        return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nLength: {}".format(self.name, self.legs, self.place, self.move, self.weight, self.length)

    def add_info(self):
        info_name = input("Enter name")
        info_legs = input("Enter how many legs this animal have")
        info_place = input("Enter where this animal live")
        info_move = input("Enter how move this animal")
        info_weight = input("Enter Weight")
        info_length = input("Enter length")

        self.name = info_name
        self.legs = info_legs
        self.place = info_place
        self.move = info_move
        self.weight = info_weight
        self.length = info_length

Now we define Dog with only the extra arguments; anything intended for the superclass methods will be passed as arbitrary keyword arguments that Dog will pass on.

class Dog(Animal):

    def __init__(self, *, feather, aggresivity, **kwargs):
        super().__init__(**kwargs)
        self.feather = feather
        self.aggresivity = aggresivity

    def __str__(self):
        x = super().__str__()
        return x + "\nFeather: {}\nAggresivity {}".format(self.feather, self.aggresivity)

    def add_info(self):
        super().add_info()
        info_feather = input("Enter are your dog have feather or not 'have' or 'haven't")
        info_aggresivity =input("Enter are your dog aggresive or passive")
        self.feather = info_feather
        self.aggresivity = info_aggresivity

    def pick_random(self):
        list_dog = ["Labrador","Bulldog","Retriever,","Poodle","Beagle","Husky"]
        random_dog = random.choice(list_dog)
        print("Your dog is :",random_dog)

Finally, we instantiate Dog using keyword arguments.

d = Dog(name="...", legs="...", ...) # etc
chepner
  • 497,756
  • 71
  • 530
  • 681
0

When initializing a class, you must provide all of the arguments required by the init method. See this great (and similarly themed) answer which illustrates the right way to initialize a instance:

class Dog:
    def __init__(self, legs, colour):
        self.legs = legs
        self.colour = colour

fido = Dog(4, "brown")
spot = Dog(3, "mostly yellow")

In your case, you need to provide all of the required arguments: name, legs, place, move, weight, length, feather, aggresivity.

If you'd like to write a function which helps a user instantiate an instance, you could create a classmethod:

class Animal:
    ...

    @classmethod
    def add_info(cls):
        info_name = input("Enter name")
        info_legs = input("Enter how many legs this animal have")
        info_place = input("Enter where this animal live")
        info_move = input("Enter how move this animal")
        info_weight = input("Enter Weight")
        info_lenght =input("Enter lenght")

        return cls(
            info_name,
            info_legs,
            info_place,
            info_move,
            info_weight,
            info_lenght
        )

now, calling Animal.add_info() will prompt the user for the correct attributes and then return a properly instantiated object.

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
0

This answer is based on the information in my comments above:

import random
class animal():

    def __init__(self,name,legs,place,move,weight,length):
        self.name = name
        self.legs = legs
        self.place = place
        self.move = move
        self.weight = weight
        self.length = length

    def __str__(self):
        return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nlength: {}".format(self,self.name,self.legs,self.place,self.move,self.weight,self.length)
    def add_info(self):
        info_name = input("Enter name")
        info_legs = input("Enter how many legs this animal have")
        info_place = input("Enter where this animal live")
        info_move = input("Enter how move this animal")
        info_weight = input("Enter Weight")
        info_length =input("Enter length")
        self.name = info_name
        self.legs = info_legs
        self.place = info_place
        self.move = info_move
        self.weight = info_weight
        self.length = info_length

class Dog(animal):

    def __init__(self, name="sparky", legs=4, place="right here", move=True, \
            weight="45 pounds", length="30 inches", feather=False, aggresivity="friendly"):
        super().__init__(name, legs, place ,move, weight, length)
        self.feather = feather
        self.aggresivity = aggresivity
    def __str__(self):
        return "Name: {}\nLegs: {}\nPlace: {}\nMove: {}\nWeight: {}\nlength: {}\nFeather: {}\nAggresivity {}".format(self, self.name, self.legs,self.place, self.move,self.weight, self.length,self.feather,self.aggresivity)
    def add_info(self):
        super().add_info()
        info_feather = input("Enter are your dog have feather or not 'have' or 'haven't")
        info_aggresivity =input("Enter are your dog aggresive or passive")
        self.feather = info_feather
        self.aggresivity = info_aggresivity
    def pick_random(self):
        list_dog = ["Labrador","Bulldog","Retriever,","Poodle","Beagle","Husky"]
        random_dog = random.choice(list_dog)
        print("Your dog is :",random_dog)
Dog = Dog()
print("""
1 for add info to your dog
2 for get infos your dog have
3 for pick random dog type
q for quit
""")
while True:
    choice = input("Enter operation: ")
    if (choice =="q"):
        print("BYE...")
        break
    elif(choice == "1"):
        Dog.add_info()

    elif(choice =="2"):
        pass

    elif(choice =="3"):
        pass

    else:
        print("İnvalid operation")
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39