0
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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

1

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 ideally returns not to print, (It's a best practice always return from the function, rather than using)
  • self.color not an attribute of Dog class

Output:

Philo's coat is brown.
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
  • Sir,I am sorry I don't understand your answer. Can you kindly be more specific. I am new to oop. – user5292153 Oct 16 '22 at 15:11
  • @user5292153 The bullet points are the issues of your code. Which one you didn't understand? – Rahul K P Oct 16 '22 at 15:31
  • I do not understand your second bullet. For example, The other methods such as "speak" returns a print. What is special about coat_color_print? It is like any other method. Please do not be cryptic. I am new to oop. If you can recommend a good book on oop, please do. Thanks. – user5292153 Oct 17 '22 at 18:17
  • @user5292153 When I call this `philo.coat_color_print()` it will return `f"{self.name}'s coat is {self.coat_color}."` and it's do this it's print(f"{self.name}'s coat is {self.coat_color}."`) – Rahul K P Oct 17 '22 at 18:26