Home > Software design >  Does anyone know how to make this work it is for object orientated programming?
Does anyone know how to make this work it is for object orientated programming?

Time:10-27

The threat level is calculated correctly but the rest of the attributes are either not displayed or in the wrong order.enter image description here

Here is the code if u can help me:

class animal():
    def __init__(self, animal_species="unknown", age=0, threat_level="peaceful", hunger_level=0): 
        
        self.animal_species = animal_species
        self.age = age
        self.hunger_level = hunger_level
        self.threat_level = threat_level

def create():
    animal_species = input("Enter species: ")
    age = int(input("Enter age: "))
    return animal(animal_species, age)

def changeThreat_level():
    hunger_level = int(input("Enter hunger(1-10): "))
    threat_level = None
    if hunger_level <= 3:
      threat_level = 'peaceful'
    elif hunger_level >=4 and hunger_level<=7:
      threat_level = 'narky'
    else:
      threat_level = 'aggressive'
    return animal(hunger_level, threat_level)


cat = create()
cat = changeThreat_level()

print("Animal's Attributes: ")
print("-------------------------")
print("The species of the animal is", cat.animal_species)
print("The age of the animal is", cat.age)
print("The animals threat level is", cat.threat_level)
print("The animals hunger out of 10 is around", cat.hunger_level)

CodePudding user response:

Your return in changeThreat_level is putting hunger_level and threat_level values where the constructor is expecting animal_species and age. You could try specifying the fields:

return animal(hunger_level=hunger_level, threat_level=threat_level)

Better yet pass in the instance of the animal and change it's attributes. Something like this should work:

class animal():
    def __init__(self, animal_species="unknown", age=0, threat_level="peaceful", hunger_level=0): 
        
        self.animal_species = animal_species
        self.age = age
        self.hunger_level = hunger_level
        self.threat_level = threat_level

def create():
    animal_species = input("Enter species: ")
    age = int(input("Enter age: "))
    return animal(animal_species, age)

def changeThreat_level(animal:animal):
    hunger_level = int(input("Enter hunger(1-10): "))
    threat_level = None
    if hunger_level <= 3:
      threat_level = 'peaceful'
    elif hunger_level >=4 and hunger_level<=7:
      threat_level = 'narky'
    else:
      threat_level = 'aggressive'
    animal.hunger_level = hunger_level
    animal.threat_level = threat_level


cat = create()
changeThreat_level(cat)

print("Animal's Attributes: ")
print("-------------------------")
print("The species of the animal is", cat.animal_species)
print("The age of the animal is", cat.age)
print("The animals threat level is", cat.threat_level)
print("The animals hunger out of 10 is around", cat.hunger_level)

CodePudding user response:

One of OOP principles is encapsulation, which is achieved when each object keeps its state private inside the class. Consider using getters and setters for objects manipulation. In this way you won't face this kind of problem.

Also, your identation is wrong, as create and changeThreat_level are methods of the Cat object, they must be nested below the class. Also, as the cat is an instance of the animal object consider initializing cat as the following:

class animal():
    def __init__(self, animal_species="unknown", age=0, threat_level="peaceful", hunger_level=0): 
        self.animal_species = animal_species
        self.age = age
        self.hunger_level = hunger_level
        self.threat_level = threat_level

    def create(self):
        self.animal_species = input("Enter species: ")
        self.age = int(input("Enter age: "))
        self.hunger_level = int(input("Enter hunger(1-10): "))

    def change_threat_level(self):
        if self.hunger_level <= 3:
            self.threat_level = 'peaceful'
        elif self.hunger_level >=4 and self.hunger_level<=7:
            self.threat_level = 'narky'
        else:
            self.threat_level = 'aggressive'

    def print_animal_information(self):
        print("Animal's Attributes: ")
        print("-------------------------")
        print("The species of the animal is", self.animal_species)
        print("The age of the animal is", self.age)
        print("The animals threat level is", self.threat_level)
        print("The animals hunger out of 10 is around", self.hunger_level)


cat = animal()
cat.create()
cat.change_threat_level()
cat.print_animal_information()

Now your code works fine.

  • Related