class Temperature:
def __init__(self, temperature):
"""Gets temperature from user"""
self.__temperature = temperature
def aboveFreezing(self):
"""Returns True if temperature above freezing point"""
raise NotImplementedError("Method aboveFreezing not implemented")
def convertToFahren(self):
"""Returns a new Temperature object converted to degrees Fahrenheit"""
raise NotImplementedError("Method convertToFahren not implemented")
def convertToCelsius(self):
"""Returns a new Temperature object converted to degrees Celsius"""
raise NotImplementedError("Method convertToCelsius not implemented")
def convertToKelvin(self):
"""Returns a new Temperature object converted to degrees Kelvin"""
raise NotImplementedError("Method convertToKelvin not implemented")
class Kelvin(Temperature):
def __init__(self, temperature):
Temperature.__init__(self,temperature)
def __str__(self):
"""Returns string of temperature"""
return self.__temperature + 'degrees Kelvin.'
def aboveFreezing(self):
"""Returns True if temperature above freezing point"""
if self.__temperature > 273.15:
return True
else:
return False
def convertToFahren(self):
"""Returns a copy Kevin to Fahren"""
return ((self.__temperature * 9/5) - 459.67)
def convertToCelsius(self):
"""Returns conversion from Kelvin to Celsius"""
return self.__temperature - 273.15
def convertToKelvin(self):
"""Returns copy of Kelvin"""
return self.__temperature
I try to run the Kelvin class so I try:
t1 = Kelvin(33)
print(t1)
But it tells me that:
return self.__temperature + 'degrees Kelvin.'
AttributeError: 'Kelvin' object has no attribute '_Kelvin__temperature'
I'm new to classes. I think it is because I have an error in:
def __init__(self, temperature):
Temperature.__init__(self,temperature)
So if this is the problem, how do I get the Kelvin class to have the value of temperature? I have to make Kelvin a subclass of Temperature. If I didn't have to do that I know I could do: self.__temperature = temperature
But how I assign the temperature from the Temperature class to the subclass Kelvin?