Low-level methods with temperature conversion program This is my first program to convert temperatures. I have a very basic knowledge of Python and I tried using only what I already knew to make the program. What are some improvements I can make, or how can I re-create this in an easier way?

temp_string = raw_input("What is the Temperature: ")

x = int(temp_string)

print x

temp_type = raw_input("Would you like to convert to Celsius or Fahrenheit?: ")

input_type = str(temp_type)

def convert_fahrenheit(x):
    x = (x - 32) * 0.556
    print x

def convert_celsius(x):
    x = (x * 1.8) + 32
    print x

if input_type == str("c"):
    convert_fahrenheit(x)
elif input_type == str("celsius"):
    convert_fahrenheit(x)
elif input_type == str("Celsius"):
    convert_fahrenheit(x)
elif input_type == str("C"):
    convert_celsius(x)
elif input_type == str("f"):
    convert_celsius(x)
elif input_type == str("fahrenheit"):
    convert_celsius(x)
elif input_type == str("Fahrenheit"):
    convert_celsius(x)
elif input_type == str("F"):
    convert_celsius(x)
else:
    print "You have not entered a valid conversion"
Overall I think the most important thing to improve here is code clarity.

You have some code like this:

if input_type == str("c"):


"c" is already a string so doing str("c") is redundant. It's definitely easier and clearer to just write:

if input_type == "c":


There is also a lot of boilerplate to call the required functions with that big if-else block. Instead I would use a dictionary here to do this:

temp_input = int(raw_input("What is the Temperature: "))

input_type = str(raw_input("Would you like to convert to Celsius or Fahrenheit?: "))

conversion_dispatcher = {
    "c": convert_fahrenheit,
    "C": convert_fahrenheit,
    "celsius": convert_fahrenheit,
    "Celsius": convert_fahrenheit,
    "f": convert_celsius,
    "F": convert_celsius,
    "fahrenheit": convert_celsius,
    "Fahrenheit": convert_celsius,
}

try:
    conversion_dispatcher[input_type](temp_input)
except KeyError:
    print("You have not entered a valid conversion")


This way you end up with a lot less boilerplate overall and it's much easier to make changes if you find a problem.

Also I would change the conversion functions to return a value and I would print that value afterwards:

def convert_fahrenheit(x):
    x = (x - 32) * 0.556
    return x

def convert_celsius(x):
    x = (x * 1.8) + 32
    return x

try:
    new_value = conversion_dispatcher[input_type](temp_input)
    print(new_value)
except KeyError:
    print("You have not entered a valid conversion")


The reason for this is because it more clearly separates out the logic from the display code. The makes those conversion functions more reusable as you can now use them in situations where you don't just want to print to the console.

You might also have noticed that I have changed your print syntax to the new print function syntax. This is because the old print statement is going away in newer version of python (python 3.x) and you might as well get used to writing it the other way now. If this causes an error in your version of python you might need to use from __future__ import print_function at the top of your file.

Now that we are done with the Python related stuff it's probably good to improve some other elements of the code. Given the function name isn't completely unambiguous a docstring explaining which way the units conversion is being applied in the conversion functions would help future readers of your code understand what's going on better. This is especially the case because your printout at the beginning suggests different behavior to the code you wrote.

For example:

def convert_to_celsius(x):
    """Converts a temperature in Celsius to Fahrenheit"""
    x = (x * 1.8) + 32
    return x