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 ofDog
class
Output:
Philo's coat is brown.