Write a Python class Temp that meets the following requirements: Attributes: _te
ID: 3726961 • Letter: W
Question
Write a Python class Temp that meets the following requirements:
Attributes:
_temp: the numerical value of the temperature the object represents;
_unit: "C" if the object was created using a Celsius temperature; "F" if it was created using a Fahrenheit temperature.
Methods:
__init__(self, temp, unit): where temp is a number and unit is either "C" or "F" depending on whether temprepresents a Celsius or Fahrenheit temperature. This method initializes the _temp attribute of the object with the value of temp and the _unit attribute with the value of unit.
__str__(self): returns a string consisting of the value of the object's _temp attribute together with the unit ("C" or "F") the object was created with.
Methods that take a second Temp object other as argument and compare the temperature represented by the object is hotter, cooler, or the same as that represented by other.
Given two Temp objects t1 and t2, it should be possible to determine relationship between them, i.e., whether they represent the same temperature, or whether one is a bigger or smaller temperature when converted to the same unit, using the comparison operations <, , >, ,==, !=.
Explanation / Answer
class Temp: def __init__(self, temp, unit): self._temp = temp self._unit = unit def __str__(self): return self._temp + ' ' + self._unit def __eq__(self, other): t1 = self._temp t2 = other._temp if self._unit == 'C': t1 = (t1 * 1.8) + 32 if other._unit == 'C': t2 = (t2 * 1.8) + 32 return t1 == t2 def __lt__(self, other): t1 = self._temp t2 = other._temp if self._unit == 'C': t1 = (t1 * 1.8) + 32 if other._unit == 'C': t2 = (t2 * 1.8) + 32 return t1 = t2 def __ne__(self, other): t1 = self._temp t2 = other._temp if self._unit == 'C': t1 = (t1 * 1.8) + 32 if other._unit == 'C': t2 = (t2 * 1.8) + 32 return t1 != t2Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.