Classes and objects are tools for completing a task — they allow you to encapsulate data or state using a set of methods. However, your data is just a number. There is no need to encapsulate an integer, so there is no need to create a class.
In other words, do not create a class because, in your opinion, you should create a class because it makes your code simpler.
import sys def f_to_c(x): return (x - 32) * (5/9) def c_to_f(x): return x * (9/5) + 32 num_from = float(input('Enter a number to convert: ')) unit_from = input('What units would you like to convert from? ') unit_to = input('What units would you like to convert to? ') if (unit_from, unit_to) == ('fahrenheit', 'celsius'): num_to = f_to_c(num_from) elif (unit_from, unit_to) == ('celsius', 'fahrenheit'): num_to = c_to_f(num_from) else: print('unsupported units') sys.exit(1) print('{} degrees {} is {} degrees {}' .format(num_from, unit_from, num_to, unit_to))
Enter a number to convert: 40
What units would you like to convert from? celsius
What units would you like to convert to? fahrenheit
40.0 degrees celsius is 104.0 degrees fahrenheit
The convert object and Converter not intended for any purpose, so the code is simpler and easier to read without them.
source share