I'm doing some OOP in Python and I'm having trouble when the user inputs a tuple as one of the arguments. Here's the code:
class Height:
def __init__(self,ft,inch=0):
if isinstance(ft,tuple):
self.feet = ft
self.inches = inch
elif isinstance(ft,int):
self.feet = ft // 12
self.inches = ft % 12
def __str__(self):
return str(self.feet) + " Feet " + str(self.inches) + " Inches"
def __repr__(self):
return "Height (" + str(self.feet * 12 + self.inches) + ")"
I tried thought initializing the inch to 0 would help thing out but that didn't work. Tuples also don't support indexing so that option was also non-existent. I feel like the answer is simple and I'm just overthinking it. The test code that I'm using is:
from height import *
def test(ht):
"""tests the __str__, __repr__, and to_feet methods for the height
Height->None"""
#print("In inches: " + str(ht.to_inches()))
#print("In feet and inches: " + str(ht.to_feet_and_inches()))
print("Convert to string: " + str(ht))
print("Internal representation: " + repr(ht))
print()
print("Creating ht1: Height(5,6)...")
ht1 = Height(5,6)
test(ht1)
print("Creating ht2: Height(4,13)...")
ht2 = Height(4,13)
test(ht2)
print("Creating ht3: Height(50)...")
ht3 = Height(50)
test(ht3)
My code works as expected when an int
is input but, again, I can't seem to figure it out when a tuple is input. Any ideas?