I'm a very early-stage python programmer, and I wrote this simple script to convert miles to kilometers or kilometers to miles:
metrics_dict = {("miles", "kilometers"):1.609344, ("kilometers","miles"):0.62137119}
number = float(raw_input("Enter the number of units you would like to convert: "))
from_metric = raw_input("I would like to convert from (miles or kilomters): ")
to_metric = raw_input("to(miles or kilomters): ")
conversion = metrics_dict.get((from_metric,to_metric), "You messed up somewhere!")
def convert_func():
answer = number * conversion
print number , from_metric, "is equal to", answer , to_metric
convert_func()
I'm sure this code is ugly. How could I better structure it?
Also, how do I run the entire thing? It only works if I run each line separately in IDLE.