Home > Net >  Why one of the methods does not complete to execution?
Why one of the methods does not complete to execution?

Time:10-15

class Dog:
    species = "Canis familiaris"
    
    
    def __init__(self, name, age, color):
        self.name = name
        self.age = age
        self.coat_color = color
        
# Instance method
    def description(self):
        return f"{self.name} is {self.age} years old"
    
    def __str__(self):
        return f"{self.name} is {self.age} years old"
    
# Another instance method

    def speak(self, sound):
        return f"{self.name} says {sound}"
    
#  Coat Color
    def coat_color(self, color):
        print(f"{self.name}'s coat is {self.color}.")

philo = Dog("Philo", 5, "brown")      # Instantiate

print(philo.coat_color)

## brown                     #  Response

Why is the second line print(f"{self.name}'s coat is {self.color}.") not getting executed?

CodePudding user response:

print(philo.coat_color())

Don't forget the () parentheses to call the function.

CodePudding user response:

There couple of issue in your code,

class Dog:
    species = "Canis familiaris"
           
    def __init__(self, name, age, color):
        self.name = name
        self.age = age
        self.coat_color = color
        
    def coat_color_print(self):
        return f"{self.name}'s coat is {self.coat_color}."


philo = Dog("Philo", 5, "brown")
print(philo.coat_color_print())
  • You gave coat_color for the attribute and method.
  • A method ideallyreturns not print
  • self.color not an attribute of Dog class

Output:

Philo's coat is brown.
  • Related