class FootMeasure(object):
def __init__(self, feet=0, inches=0):
"""Initiates feet and inches to 0. User can enter custom numbers"""
self.__feet = feet
self.__inches = inches
def __repr__(self):
"""Returns inches higher than 11 to a foot representation
Returns 60 inches at 5 ft instead of 5 ft 0 inches
Returns 0 as 0 ft 0 inches
"""
if self.__inches == 0:
return str(self.__feet) + ' ft '
elif self.__inches > 11:
feet = int(self.__inches / 12)
remainder = self.__inches % 12
return str(feet) + ' ft ' + str(remainder) + ' in '
else:
return str(self.__feet) + ' ft. ' + str(self.__inches) + ' in. '
def __add__(self):
"""Returns two FootMeasures added together"""
I am new to classes so I am a bit confused on how to do this. I know it is probably some simple process but I am only finding examples on adding fractions and can't figure it out. So, how would I make an add method for this? For instance if I said first = FootMeasure(1,1) and second = FootMeasure(1,1). Then result = first + second. And I of course wanted the result of 2 ft 2 in. How could I do this?
3 Answers 3
The __add__ method should take two arguments: one for the object itself, and one for the other one. Then add the appropriate values together and return the result.
However, I would recommend refactoring your code to store a FootMeasure object as a single number of inches. You can then do calculations much more easily. This is similar to how currency calculations are handled, with values represented by a single integer of the smallest denomination (e.g. 3ドル.50 would be represented as 350 cents).
Also, an object's __repr__ is supposed to allow you to reproduce the object, in a way that eval() can handle. You can use the built-in divmod() to produce the quotient and remainder, then unpack it for the format string.
Finally, this class would do well with other methods for a nicely-formatted string (__str__, the contents of which you had in __repr__), the object's feet, the object's inches (inches under 12 taking feet into account, not total inches, as that's already inches), and so on.
class FootMeasure(object):
def __init__(self, feet=0, inches=0):
"""Initiates feet and inches to 0. User can enter custom numbers"""
self.inches = feet*12 + inches
def __repr__(self):
"""Returns the string representation of the object, in feet and inches
"""
return 'FootMeasure({}, {})'.format(*divmod(self.inches, 12))
def __add__(self, other):
"""Returns two FootMeasures added together"""
return FootMeasure(inches=self.inches + other.inches)
Comments
You need two arguments, self and other. Then for each new attribute you just add the attribute of self and other together. Create and return a new FootMeasure object with those new values, and you're all set!
def __add__(self, other):
"""Returns two FootMeasures added together"""
return FootMeasure(self.__feet + other.__feet,
self.__inches + other.__inches)
Comments
You need two arguments:
def __add__(self, other):
(feet, inches)pairs? Do that, but call it__add__(self, other), make it take FootMeasures as self and other, and build a FootMeasure instead of a tuple as the return value.