convert between different units
Thu Jan 19 2023 07:33:59 GMT+0000 (Coordinated Universal Time)
Saved by @Shaghaf_2000 #python
# Task1
def main():
#thisfunction will allow the user to choose which function to run:
# 1. print options out:
print("Options: ")
print("Temperature = convert between two different temperature units")
print("Length = convert between two different length units")
print("Mass = convert between two different mass units")
print("Area = convert between two different area units")
option = input("Select an option or control C to exit the program: ")
option=option.lower()
# to allow user repeat:
while True:
# to allow user to quit:
try:
if option == 'temperature':
temperature_convert()
elif option == 'length':
length_convert()
elif option == 'mass':
mass_convert()
elif option=='area':
area_convrt()
else:
print('Invalid Inputs')
except KeyboardInterrupt:
print("\nExiting program\n")
break
def temperature_convert():
#temperature = ['Kelvin', 'Celsius','Fahrenheit', 'Rankine', 'Delisle']
validUnit=['C', 'K', 'F', 'R','D']
temperature = {'K':'Kelvin','C':'Celsius','F':'Fahrenheit','R':'Rankine', 'D':'Delisle'}
# print(temperature)
# print(validUnit)
convertFrom = input("Enter unit to convert from (C = Celcius, K = Kelvin, F = Fahrenheit, R = Rankine, D = Delisle): ")
converTo = input("Enter unit to convert to (C = Celcius, K = Kelvin, F = Fahrenheit, R = Rankine, D = Delisle): ")
value = input("Enter temperature value to convert: ")
while True:
try:
convertFrom = str(convertFrom)
converTo = str(converTo)
value = float(value)
convertFrom = convertFrom.upper()
converTo = converTo.upper()
if convertFrom not in validUnit or converTo not in validUnit:
print("Invalid Unit! ")
else:
break
except ValueError:
print("Conversion faild")
"""
# From Kelvin:
if convertFrom == 'Kelvin' and converTo == 'Celsius':
newValue = value-273.15
print(f"Temperature converted from {temperature[0]}K to {temperature[1]}°C is {round(newValue,2)}°C")
elif convertFrom == 'Kelvin' and converTo == 'Fahrenheit':
newValue= (((value-273.15)**9)/5)+32
print(f"Temperature converted from {temperature[0]}K to {temperature[2]}°F is {round(newValue,2)}°F")
elif convertFrom == 'Kelvin' and converTo =='Rankine':
newValue = (value*9)/5
print(f"Temperature converted from {temperature[0]}K to {temperature[3]}°R is {round(newValue,2)}°R")
elif convertFrom == 'Kelvin' and converTo =='Delisle':
newValue = ((373.15 - value)*3)/2
print(f"Temperature converted from {temperature[0]}K to {temperature[4]}°D is {round(newValue,2)}°D")
# From Celsius
elif convertFrom =='Celsius' and converTo=='Kelvin':
newValue= value+273.15
print(f"Temperature converted from {temperature[1]}°C to {temperature[0]}K is {round(newValue,2)}K")
elif convertFrom == 'Celsius' and converTo == 'Fahrenheit':
newValue = (value*9/5)+3**2
print(f"Temperature converted from {temperature[1]}°C to {temperature[2]}°F is {round(newValue,2)}°F")
elif convertFrom == 'Celsius' and converTo =='Rankine':
newValue = (value*9/5)+491.67
print(f"Temperature converted from {temperature[1]}°C to {temperature[3]}°R is {round(newValue,2)}°R")
elif convertFrom == 'Celsius' and converTo == temperature[4]:
newValue = ((100-value)*3)/2
print(f"Temperature converted from {temperature[1]}°C to {temperature[4]}°D is {round(newValue,2)}°D ")
# from Fahrenheit:
elif convertFrom == temperature [2] and converTo =='Kelvin':
newValue = (value+459.67)*5/9
print(f"Temperature converted from {temperature[1]}°F to {temperature[0]}K is {round(newValue,2)}K")
elif convertFrom == temperature [2] and converTo =='Celsius':
newValue = (value -32)*5/9
print(f"Temperature converted from {temperature[1]}°F to {temperature[1]}°C is {round(newValue,2)}°C ")
elif convertFrom == temperature [2] and converTo =='Rankine':
newValue = value + 459.67
print(f"Temperature converted from {temperature[1]}°F to {temperature[3]}°R is {round(newValue,2)}°R ")
elif convertFrom == temperature [2] and converTo == 'Delisle':
newValue = (212 -value) * 5/6
print(f"Temperature converted from {temperature[1]}°F to {temperature[4]}°D is {round(newValue,2)}°D ")
# from Rankine (°R):
elif convertFrom == 'Rankine' and converTo == 'Kelvin':
newValue = value/1.8
print(f"Temperature converted from {temperature[3]}°R to {temperature[0]}K is {round(newValue,2)}K")
elif convertFrom == 'Rankine' and converTo =='Celsius':
newValue = (value-491.67)*5/9
print(f"Temperature converted from {temperature[3]}°R to {temperature[1]}°C is {round(newValue,2)}°C ")
elif convertFrom == 'Rankine' and converTo =='Fahrenheit':
newValue = value-459.67
print(f"Temperature converted from {temperature[3]}°R to {temperature[2]}°F is {round(newValue,2)}°F ")
elif convertFrom == 'Rankine' and converTo == 'Delisle':
newValue = (value-491.67) * 0.83333
print(f"Temperature converted from {temperature[3]}°R to {temperature[4]}°D is {round(newValue,2)}°D ")
# FROM D:
elif convertFrom == 'Delisle' and converTo =='Kelvin':
newValue = (373.15-value)*2/3
print(f"Temperature converted from {temperature[4]}°D to {temperature[0]}K is {round(newValue,2)}K")
elif convertFrom == 'Delisle' and converTo == 'Celsius':
newValue = (value+100)/1.5000
print(f"Temperature converted from {temperature[4]}°D to {temperature[1]}°C is {round(newValue,2)}°C ")
elif convertFrom == 'Delisle' and converTo =='Fahrenheit':
newValue = ((value+100)*1.2000)+32
print(f"Temperature converted from {temperature[4]}°D to {temperature[2]}°F is {round(newValue,2)}°F ")
elif convertFrom == 'Delisle' and converTo == 'Rankine':
newValue = ((value+100)*1.2000 )+491.67
print(f"Temperature converted from {temperature[4]}°D to {temperature[3]}°R is {round(newValue,2)}°D ")
else:
print("Invalid inputs. ")
"""
# From Kelvin:
if convertFrom == 'K' and converTo == 'C':
newValue = value-273.15
print(f"Temperature converted from ({temperature[convertFrom]}) K to ({temperature[converTo]}) °C is {round(newValue,2)}°C")
elif convertFrom == 'K' and converTo == 'F':
newValue= (((value-273.15)**9)/5)+32
print(f"Temperature converted from ({temperature[convertFrom]}) K to ({temperature[converTo]}) °F is {round(newValue,2)}°F")
elif convertFrom == 'K' and converTo =='R':
newValue = (value*9)/5
print(f"Temperature converted from ({temperature[convertFrom]}) K to ({temperature[converTo]}) °R is {round(newValue,2)}°R")
elif convertFrom == 'K' and converTo =='D':
newValue = ((373.15 - value)*3)/2
print(f"Temperature converted from ({temperature[convertFrom]}) K to ({temperature[converTo]}) °D is {round(newValue,2)}°D")
# From Celsius
elif convertFrom =='C' and converTo=='K':
newValue= value+273.15
print(f"Temperature converted from ({temperature[convertFrom]}) °C to {temperature[converTo]} K is {round(newValue,2)}K")
elif convertFrom == 'C' and converTo == 'F':
newValue = (value*9/5)+3**2
print(f"Temperature converted from ({temperature[convertFrom]}) °C to ({temperature[converTo]}) °F is {round(newValue,2)}°F")
elif convertFrom == 'C' and converTo =='R':
newValue = (value*9/5)+491.67
print(f"Temperature converted from ({temperature[convertFrom]}) °C to {temperature[converTo]}°R is {round(newValue,2)}°R")
elif convertFrom == 'C' and converTo == 'D':
newValue = ((100-value)*3)/2
print(f"Temperature converted from ({temperature[convertFrom]}) °C to ({temperature[converTo]}) °D is {round(newValue,2)}°D ")
# from Fahrenheit:
elif convertFrom == 'F'and converTo =='K':
newValue = (value+459.67)*5/9
print(f"Temperature converted from ({temperature[convertFrom]}) °F to ({temperature[converTo]}) K is {round(newValue,2)}K")
elif convertFrom == 'F' and converTo =='C':
newValue = (value -32)*5/9
print(f"Temperature converted from ({temperature[convertFrom]}) °F to ({temperature[converTo]}) °C is {round(newValue,2)}°C ")
elif convertFrom == 'F' and converTo =='R':
newValue = value + 459.67
print(f"Temperature converted from ({temperature[convertFrom]}) °F to ({temperature[converTo]}) °R is {round(newValue,2)}°R ")
elif convertFrom == 'F' and converTo == 'D':
newValue = (212 -value) * 5/6
print(f"Temperature converted from {temperature[convertFrom]} °F to {temperature[converTo]} °D is {round(newValue,2)}°D ")
# from Rankine (°R):
elif convertFrom == 'R' and converTo == 'K':
newValue = value/1.8
print(f"Temperature converted from {temperature[convertFrom]} °R to {temperature[converTo]} K is {round(newValue,2)}K")
elif convertFrom == 'R' and converTo =='C':
newValue = (value-491.67)*5/9
print(f"Temperature converted from ({temperature[convertFrom]}) °R to ({temperature[converTo]}) °C is {round(newValue,2)}°C ")
elif convertFrom == 'R' and converTo =='F':
newValue = value-459.67
print(f"Temperature converted from ({temperature[convertFrom]}) °R to ({temperature[converTo]}) °F is {round(newValue,2)}°F ")
elif convertFrom == 'R' and converTo == 'D':
newValue = (value-491.67) * 0.83333
print(f"Temperature converted from ({temperature[convertFrom]}) °R to ({temperature[converTo]}) °D is {round(newValue,2)}°D ")
# From D:
elif convertFrom == 'D' and converTo =='K':
newValue = (373.15-value)*2/3
print(f"Temperature converted from ({temperature[convertFrom]}) °D to ({temperature[converTo]}) K is {round(newValue,2)}K")
elif convertFrom == 'D' and converTo == 'C':
newValue = (value+100)/1.5000
print(f"Temperature converted from ({temperature[convertFrom]}) °D to ({temperature[converTo]}) °C is {round(newValue,2)}°C ")
elif convertFrom == 'D' and converTo =='F':
newValue = ((value+100)*1.2000)+32
print(f"Temperature converted from ({temperature[convertFrom]}) °D to ({temperature[converTo]}) °F is {round(newValue,2)}°F ")
elif convertFrom == 'D' and converTo == 'R':
newValue = ((value+100)*1.2000 )+491.67
print(f"Temperature converted from ({temperature[convertFrom]}) °D to ({temperature[converTo]}) °R is {round(newValue,2)}°D ")
else:
print("Invalid inputs. ")
#temperature_convert()
def length_convert():
# Take user inputs:
value = input("Enter length value: ")
convertFrom = input("Enter unit you want to convert from: ")
converTo = input("Enter unit you want to convert to: ")
# creat list that contains units:
validUnits = ['m','in','ft','yd','mi','nm']
# create a dictionary that has letters with their coressponding unit names:
unitNames = {'m':'Metre',
'in': 'Inch',
'ft':'Feet',
'yd': 'Yard',
'mi':'Mile',
'nm':'Nautical Mile'
}
converTo=converTo.lower()
convertFrom=convertFrom.lower()
# Validation:
while True:
try:
value = float(value)
if convertFrom not in validUnits or converTo not in validUnits:
print("Unit Invalid. ")
else:
break
except ValueError:
print("Conversion faild")
# Conver from meter:
if convertFrom == 'm' and converTo == 'in':
newvalue = value*39.3701
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} in. ")
elif convertFrom == 'm' and converTo == 'ft':
newvalue=value*3.28084
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} ft. ")
elif convertFrom == 'm' and converTo == 'yd':
newvalue=value*1.094
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} yd. ")
elif convertFrom == 'm' and converTo == 'mi':
newvalue=value/1609
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} mi. ")
elif convertFrom == 'm' and converTo == 'nm':
newvalue=value*0.000539957
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} nm. ")
# convert from inch:
if convertFrom == 'in' and converTo == 'm':
newvalue = value/39.37
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} m. ")
elif convertFrom == 'in' and converTo == 'ft':
newvalue=value/12
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} ft. ")
elif convertFrom == 'in' and converTo == 'yd':
newvalue=value/36
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} yd. ")
elif convertFrom == 'in' and converTo == 'mi':
newvalue=value/63360
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} mi. ")
elif convertFrom == 'in' and converTo == 'nm':
newvalue=value/72910
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} nm. ")
# convert from feet:
if convertFrom == 'ft' and converTo == 'in':
newvalue =value*12
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} in. ")
elif convertFrom == 'ft' and converTo == 'm':
newvalue=value/3.281
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} m. ")
elif convertFrom == 'ft' and converTo == 'yd':
newvalue=value/3
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} yd. ")
elif convertFrom == 'ft' and converTo == 'mi':
newvalue=value/5280
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} mi. ")
elif convertFrom == 'ft' and converTo == 'nm':
newvalue=value/6076
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} nm. ")
# convert from Yard:
if convertFrom == 'yd' and converTo == 'in':
newvalue =value*36
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} in. ")
elif convertFrom == 'yd' and converTo == 'm':
newvalue=value/1.094
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} m. ")
elif convertFrom == 'yd' and converTo == 'ft':
newvalue=value*3
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} ft. ")
elif convertFrom == 'yd' and converTo == 'mi':
newvalue=value/1760
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} mi. ")
elif convertFrom == 'yd' and converTo == 'nm':
newvalue=value/2025
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} nm. ")
# convert from Mile:
if convertFrom == 'mi' and converTo == 'in':
newvalue =value*63360
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} in. ")
elif convertFrom == 'mi' and converTo == 'm':
newvalue=value*1609
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} m. ")
elif convertFrom == 'mi' and converTo == 'ft':
newvalue=value*5280
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} ft. ")
elif convertFrom == 'mi' and converTo == 'yd':
newvalue=value*1760
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} yd. ")
elif convertFrom == 'mi' and converTo == 'nm':
newvalue=value/1.151
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} nm. ")
# convert from Nautical Mile:
if convertFrom == 'nm' and converTo == 'in':
newvalue =value*72910
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} in. ")
elif convertFrom == 'nm' and converTo == 'm':
newvalue=value*1852
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} m. ")
elif convertFrom == 'nm' and converTo == 'ft':
newvalue=value*6076
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} ft. ")
elif convertFrom == 'nm' and converTo == 'yd':
newvalue=value*2025
print(f"The length {value} converted from ({unitNames[convertFrom]}) to ({unitNames[converTo]}) is {round(newvalue,2)} yd. ")
elif convertFrom == 'nm' and converTo == 'mi':
newvalue=value*1.151
print(f"The length {value} converted from ({unitNames[convertFrom]}) to {round(newvalue,2)} mi. ")
#length_convert()
def mass_convert():
# get user inputs:
while True:
try:
value = input("Enter value of mass you want to convert: ")
convertFrom = input("Enter unit you want to convert from (Kilogram (kg), Pound (lbs),Ounce (oz), Stone (st), Tons (t)): ")
convertTo = input("Enter unit you want to convert to (Kilogram (kg), Pound (lbs),Ounce (oz), Stone (st), Tons (t)): ")
validUnits =['kg', 'lbs', 'oz', 'st','t']
unitNames = {
'kg': 'Kilogram ',
'lbs': 'Pound ',
'oz': 'Ounce',
'st': 'Stone ',
't':'Tons '
}
value = float(value)
convertFrom=convertFrom.lower()
convertTo=convertTo.lower()
if convertFrom not in validUnits or convertTo not in validUnits:
print("Invalid Units. ")
else:
break
except ValueError:
print("conversion faild")
# convert from kg:
if convertFrom == 'kg' and convertTo=='lbs':
newvalue=value*2.205
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'kg' and convertTo=='oz':
newvalue=value*35.274
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'kg' and convertTo=='st':
newvalue=value/6.35
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'kg' and convertTo=='t':
newvalue=value/1000
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
# convert from pound:
if convertFrom == 'lbs' and convertTo=='kg':
newvalue=value/2.205
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'lbs' and convertTo=='oz':
newvalue=value*16
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'lbs' and convertTo=='st':
newvalue=value/14
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'lbs' and convertTo=='t':
newvalue=value/2205
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
# convert from Ounce:
if convertFrom == 'oz' and convertTo=='kg':
newvalue=value/35.274
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'oz' and convertTo=='lbs':
newvalue=value/16
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'oz' and convertTo=='st':
newvalue=value/224
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'oz' and convertTo=='t':
newvalue=value/35270
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
# convert from Stone:
if convertFrom == 'st' and convertTo=='kg':
newvalue=value*6.35
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'st' and convertTo=='lbs':
newvalue=value*14
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'st' and convertTo=='oz':
newvalue=value*224
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 'st' and convertTo=='t':
newvalue=value/157.5
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
# convert from Tones:
if convertFrom == 't' and convertTo=='kg':
newvalue=value*1000
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 't' and convertTo=='lbs':
newvalue=value*2205
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 't' and convertTo=='oz':
newvalue=value*35270
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
elif convertFrom == 't' and convertTo=='st':
newvalue=value*157.5
print(f'Mass {value} converted from {unitNames[convertFrom]} to {unitNames[convertTo]} is {round(newvalue,2)}')
#mass_convert()
def area_convrt():
while True:
value = input("Enter area: ")
convertFrom = input("Enter unit you want to conver area from: ")
convertTo = input("Enter unit you want to conver area from: ")
validUnits= ['km2', 'm2', 'ha', 'ac','in^2','ft2','yd2']
unitNames = {
}
try:
value = float(value)
if convertFrom not in validUnits or convertTo not in validUnits:
print("Invalid unit")
else:
break
except ValueError:
print("Conversion faild")
#area_convrt()
main()



Comments