Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Temperature:
- def __init__(self, temp, scale=None):
- self.t = temp
- self.scale = scale
- self.call_temp = {'c': {'c': self, 'f': self.celsius_to_fahrenheight, 'k': self.celsius_to_kelvin},
- 'f': {'f': self, 'c': self.fahrenheight_to_celsius, 'k': self.fahrenheight_to_kelvin},
- 'k': {'k': self, 'c': self.kelvin_to_celsius, 'f': self.kelvin_to_fahrenheight}}
- def celsius(t):
- if isinstance(t, int):
- return Temperature(t, 'c')
- else:
- return t.call_temp[t.scale]['c']()
- def fahrenheight(t):
- if isinstance(t, int):
- return Temperature(t, 'f')
- else:
- return t.call_temp[t.scale]['f']()
- def kelvin(t):
- if isinstance(t, int):
- return Temperature(t, 'k')
- else:
- return t.call_temp[t.scale]['k']()
- def __call__(self):
- return self.t
- def celsius_to_fahrenheight(self):
- f = (self.t)/5 + 32
- return round(f,2)
- def celsius_to_kelvin(self):
- k = self.t + 273
- return round(k,2)
- def fahrenheight_to_celsius(self):
- c = 5*(self.t-32)/9
- return round(c,2)
- def fahrenheight_to_kelvin(self):
- k = 5*(self.t-32)/9 + 273
- return round(k,2)
- def kelvin_to_celsius(self):
- c = self.t - 273
- return round(c,2)
- def kelvin_to_fahrenheight(self):
- f = 9*(self.t)/5 + 32
- return round(f,2)
- t1 = Temperature(32)
- print(t1.celsius_to_kelvin())
- print('C 32 to K', Temperature.celsius(32).kelvin())
- c1 = Temperature.celsius(30)
- print('C 30 to K', c1.kelvin())
- print('C 30 to F', c1.fahrenheight())
- print('C 30 itself', c1())
Add Comment
Please, Sign In to add comment