Simple Python form please. 1) Define a class called Distance, for representing a
ID: 3803423 • Letter: S
Question
Simple Python form please.
1) Define a class called Distance, for representing a distance (with one attribute that represents the distance in inches). You will also need to implement the following:
The Distance class must have a constructor that takes two optional numerical arguments, representing a number of inches and a number of feet. If there are no arguments, assume that they are all 0. If there is only one argument, assume that this is the number of inches, and that the number of feet is 0. If there are two arguments, assume that the first is a number of inches and the second is a number of feet.*
So the commands Distance(3,2) and Distance(27) should both create Distance objects with 27 inches. And the command Distance() should create an instance of a Distance with 0 inches.
2) A .__repr__() method that returns a string that represents the given distance. Remember that the string you return has to look like a command that could be used to create an equivalent Distance.
Explanation / Answer
>>> dist0 = Distance()
>>> dist1 = Distance(30)
>>> dist2 = Distance(5,2)
>>> dist3 = Distance(12.5)
>>> dist0
Distance(0)
>>> dist1
Distance(30)
>>> dist2
Distance(29)
>>> dist3
Distance(12.5)
>>> print(dist1)
2'6"
>>> print(dist2)
2'5"
>>> print(dist3)
1'0.5"
>>> dist1.to_inches()
30
>>> dist1.to_feet()
2.5
>>> dist1.to_inches_and_feet()
(6, 2)
>>> dist3.to_inches()
12.5
>>> dist3.to_feet()
1.0416666666666667
>>> dist3.to_inches_and_feet()
(0.5, 1)
>>> str(dist2)
'2'5"'
>>> repr(dist2)
'Distance(29)'
>>> dist1 == dist2
False
>>> dist1 > dist2
True
>>> dist1 >= dist2
True
>>> dist1 < dist2
False
>>> dist1 <= dist2
False
>>> dist2 > dist1
False
>>> dist2 >= dist1
False
>>> dist2 < dist1
True
>>> dist2 <= dist1
True
>>> dist1 == Distance(6,2)
True
>>> dist1 > Distance(6,2)
False
>>> dist1 >= Distance(6,2)
True
>>> dist1 < Distance(6,2)
False
>>> dist1 <= Distance(6,2)
True
>>> dist1 + dist2
Distance(59)
>>> dist4 = dist2 + dist2
>>> dist4
Distance(58)
>>> print(dist4)
4'10"
>>> dist5 = feet_to_Distance(2.5)
>>> dist5
Distance(30.0)
>>> print(dist5)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.