Skip to main content
added 9 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read.

Second, you should follow the single responsibility principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(33temperature)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

You could even define another function for that last, complicated looking, bit:

def convert(temperature, from_unit, to_unit):
    return from_K_to[to_unit](to_K_from[from_unit](temperature))

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read.

Second, you should follow the single responsibility principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(33)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

You could even define another function for that last, complicated looking, bit:

def convert(temperature, from_unit, to_unit):
    return from_K_to[to_unit](to_K_from[from_unit](temperature))

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read.

Second, you should follow the single responsibility principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(temperature)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

You could even define another function for that last, complicated looking, bit:

def convert(temperature, from_unit, to_unit):
    return from_K_to[to_unit](to_K_from[from_unit](temperature))
added 91 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read.

Second, you should follow the SOLID principlesingle responsibility principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(33)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

You could even define another function for that last, complicated looking, bit:

def convert(temperature, from_unit, to_unit):
    return from_K_to[to_unit](to_K_from[from_unit](temperature))

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read.

Second, you should follow the SOLID principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(33)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

You could even define another function for that last, complicated looking, bit:

def convert(temperature, from_unit, to_unit):
    return from_K_to[to_unit](to_K_from[from_unit](temperature))

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read.

Second, you should follow the single responsibility principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(33)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

You could even define another function for that last, complicated looking, bit:

def convert(temperature, from_unit, to_unit):
    return from_K_to[to_unit](to_K_from[from_unit](temperature))
added 206 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read.

Second, you should follow the SOLID principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(33)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

You could even define another function for that last, complicated looking, bit:

def convert(temperature, from_unit, to_unit):
    return from_K_to[to_unit](to_K_from[from_unit](temperature))

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1.

Second, you should follow the SOLID principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(33)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

First of all, Python has an official style-guide, PEP8. It recommends using 4 spaces as indentation, instead of 1, which makes code a lot easier to read.

Second, you should follow the SOLID principle and define functions that do one thing, instead of having one large bunch of code.

For temperature conversion, it makes sense to first convert to one base unit (Kelvin is the obvious choice for temperature, but you might also go for Rankine if you really want to). This way you don't have to define all conversions from all units to all other units, just to and from the base unit.

Here is a start:

def temperature_C_to_K(temp_C):
    return temp_C + 273.15

def temperature_K_to_C(temp_K):
    return temp_K - 273.15

def temperature_F_to_K(temp_F):
    return 5./9 * (temp_F - 32) + 273.15

def temperature_K_to_F(temp_K):
    return 1.8 * (temp_K - 273.15) + 32

to_K_from = {"c": temperature_C_to_K,
             "f": temperature_F_to_K,
             "k": lambda t: t}
from_K_to = {"c": temperature_K_to_C,
             "f": temperature_K_to_F,
             "k": lambda t: t}

if __name__ == "__main__":
    kelvin = to_K_from["c"]
    fahrenheit = from_K_to["f"]
    temperature = 33
    print(fahrenheit(kelvin(33)))

With the additional dictionaries I defined, you can now get the user input for the to and from temperature and use these functions:

from_unit = input("From which unit? ").lower()
temperature = float(input("What temperature in that unit? "))
to_unit = input("Convert to which unit? ").lower()
print(from_K_to[to_unit](to_K_from[from_unit](temperature)))

This has no real input validation so far, so if the user entered a string for the temperature it would raise an exception and it also does not advertise which units are available, but this is left up to you.

You could even define another function for that last, complicated looking, bit:

def convert(temperature, from_unit, to_unit):
    return from_K_to[to_unit](to_K_from[from_unit](temperature))
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134
Loading