#!/usr/bin/env python3 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. """Simple program to convert from one scale to the other. Works with Celsius and Fahrenheit. Written by Cody A. Taylor , better known as 'code_m' """ __all__ = ['main', 'argv_opts', 'try_int', 'c_to_f', 'f_to_c'] __author__ = "Cody A. Taylor ( codemister99@yahoo.com )" __version__ = "3.0" import sys def main(): """Decide which conversion to run or return errors""" args = argv_opts() if args.__class__.__name__ == "int": return args temp, scale = args if scale == "f": print(f_to_c(temp)) elif scale == "c": print(c_to_f(temp)) else: print("Error: Unknown scale... Use 'C' or 'F'.") return 1 return 0 def argv_opts(): """Get needed args from the user Print Errors where needed """ if len(sys.argv) >= 3: try: temp = float(sys.argv[1]) except ValueError as err: print("Error: temp: {0}".format(err)) return 1 scale = sys.argv[2].lower() return temp, scale else: print("Usage: {0} temp scale".format(sys.argv[0]), "Description: Quickly convert Fahrenheit to Celsius.", " Quickly convert Celsius to Fahrenheit.", " 'temp' is a numerical value", " 'scale' is 'C' or 'F' respectively", sep="\n") return 0 def try_int(number): """Don't use unnessary floats""" if number.is_integer(): return int(number) return number def c_to_f(temp): """Take a temp in c and print the equivalent in f""" f = (9 / 5) f *= temp f += 32 return "{0}{2}C == {1}{2}F".format(try_int(temp), try_int(f), "\N{DEGREE SIGN}") def f_to_c(temp): """Take a temp in f and print the equivalent in c""" c = temp c -= 32 c *= (5 / 9) return "{0}{2}F == {1}{2}C".format(try_int(temp), try_int(c), "\N{DEGREE SIGN}") if __name__ == "__main__": sys.exit(main())