Start New Discussion within our Software Development Community

I am really struggling with a Python classes project.
Here are the directions

Design and implement a class called Dog that contains attributes (data) representing the dog’s name and age. Define the Dog constructor to accept and initialize that data. Include one method that will reset the dog’s age, one method that will reset the dog’s name, one method that will return the dog’s age, and one method that will return the dog’s name. These are called getters and setters. Also include a method to compute and return the age of the dog in “person years” (seven times the dog’s age). Finally, write a __str__ method that returns a one-line description of the dog. To make sure all of your class works be sure to instantiate many dog objects and invoke some of the method you wrote on these objects.

Here is my code

code
[class dog:
def __init__(self, name, age):
self.name = name
self.age = age

def setAge(self, age):
self.age = age

def getAge(self):
return self.age

def setName(self, name):
self.name = name

def getName(self):
return self.name

def getPersonAge(self):
return self.age * 7

def __str__(self):


# main

dog1 = dog("Bolt",5)]

My main question is how do I get the str method to print a message stating the name, age and human age in one sentence

You could use the format method

def __str__(self):
    return "Dog named '{0}', aged {1} (human age {2} years)".format(
        self.name, self.age, self.getPersonAge())

Also the class name should be capitalized (Dog) to conform python's tradition.

This article has been dead for over six months. Start a new discussion instead.