I'm a student currently studying computing/programming at school and am a beginner and would like some help understanding how to shorten down my code. I know some basic Python and can program relatively simple programs such as the one I need help with.My task/homework was to create a metric-imperial and vice-versa conversion calculator. I haven't quite finished it yet but how would I shorten down this long and repetitive code?
#Start
print("Welcome to the Conversion-Calculator")
In this program, "_" means "to" eg. "km_mi" is kilometres to miles
#created the menus for the length
metric_length=["'km', 'm' , 'cm'"]
imperial_length=["'mi', 'ft', 'in'"]
#This defines variables from metric to imperial measurements of distance
def km_mi():
print(leng1/1.6, "miles")
def m_ft():
print(leng1*3.26, "feet")
def cm_in():
print(leng1/2.54, "inches")
#This list defines variables from imperial to metric measurements of distance
def mi_km():
print(leng2*1.6, "kilometers")
def ft_m():
print(leng2/3.26, "metres")
def in_cm():
print(leng2*2.54, "centimetres")
#menu for weights
metric_mass=["'t','kg', 'g'"]
imperial_mass=["'stone', 'lb', 'oz'"]
#this defines variables from metric to imperial in mass
def t_st():
print(mass1*157.47, "stone")
def kg_lb():
print(mass1*2.2, "pounds")
def g_oz():
print(mass1*0.035, "ounces")
#this defines variables from imperial to metric in mass
def st_t():
print(mass2*0.00635, "tones")
def lb_kg():
print(mass2*0.4536, "kilograms")
def oz_g():
print(mass2*28.35, "grams")
#creates startup menu
menu_type=["'Length-1', 'Weight-2', 'Cacpacity-3'"]
#choose which what to convert
print(menu_type)
measure=int(input("Please select what you would like to convert by typing a number:"))
#starting if statement chain
if measure==1:
#menu to chose from either metric to imperial or vice-versa
print("Metric-Imperial - 1\n")
print("Imperial-Metric - 2\n")
choice=int(input("Choose a conversion by typing a number: "))
#If 1 is chosen follows by asking what metric distance to convert
if choice== 1:
print("Please choose the measurement you want to use.\n")
#prints the metric length menu
print(metric_length)
distance =input("Type an option from the menu: ")
#if statements that takes user input to assign a value for leng1
#then runs the function
if distance=="km":
leng1=int(input("Please enter a number to convert: "))
print=(km_mi())
elif distance=="m":
leng1=int(input("Please enter a number to convert: "))
print=(m_ft())
elif distance=="cm":
leng1=int(input("Please enter a number to convert: "))
print=(cm_in())
else:
print("Invalid Entry")
#This is the code for the second choice in the program
#If 2 is chosen follows by asking what imperial distance to convert
if choice== 2:
print("Please choose the measurement you want to use.\n")
#prints the imperial length menu
print(imperial_length)
distance2 =input("Type an option from the menu: ")
#if statements that takes user input to assign a value for leng2
#then runs the function
if distance2=="mi":
leng2=int(input("Please enter a number to convert: "))
print=(mi_km())
elif distance2=="ft":
leng2=int(input("Please enter a number to convert: "))
print=(ft_m())
elif distance2=="in":
leng2=int(input("Please enter a number to convert: "))
print=(in_cm())
else:
print("Invalid Entry")
if measure==2:
#menu to chose from either metric to imperial or vice-versa
print("Metric-Imperial - 1\n")
print("Imperial-Metric - 2\n")
choice2=int(input("Choose a conversion by typing a number: "))
#If 1 is chosen follows by asking what metric distance to convert
if choice2== 1:
print("Please choose the measurement you want to use.\n")
#prints the metric length menu
print(metric_mass)
weight =input("Type an option from the menu: ")
#if statements that takes user input to assign a value for mass1
#then runs the function
if weight=="t":
mass1=int(input("Please enter a number to convert: "))
print=(t_st())
elif weight=="kg":
mass1=int(input("Please enter a number to convert: "))
print=(kg_lb())
elif weight=="g":
mass1=int(input("Please enter a number to convert: "))
print=(g_oz())
else:
print("Invalid Entry")
#This is the code for the second choice in the program
#If 2 is chosen follows by asking what imperial mass to convert
if choice2== 2:
print("Please choose the measurement you want to use.\n")
#prints the imperial length menu
print(imperial_mass)
weight2 =input("Type an option from the menu: ")
#if statements that takes user input to assign a value for mass2
#then runs the function
if weight2=="stone":
mass2=int(input("Please enter a number to convert: "))
print=(st_t())
elif weight2=="lb":
mass2=int(input("Please enter a number to convert: "))
print=(lb_kg())
elif weight2=="oz":
mass2=int(input("Please enter a number to convert: "))
print=(oz_g())
else:
print("Invalid Entry")
('g', 'oz')
would map to the functiong_oz
. Then, instead of all thatif
/elif
stuff, you just dofunctions[source, target]()
. \$\endgroup\$leng1
and the like, pass them as parameters to the functions. This won't make things that much shorter, but it will make them a whole lot simpler to understand, and to refactor. \$\endgroup\$functions[source, target](value)
it's justprint(conversions[source, target] * value, target)
. \$\endgroup\$