In: Computer Science
PYTHON
Basic feature [7pts]Write a functionto perform unit conversion between kilograms(kg) and pounds (lb) (1 kg= 2.205 lb). The function takes 2 arguments—unit (kgor lb) and measurement—and printsthe converted valuedirectly inside the function.
Additional Features [4pts]
1.The function prints an error message when an inappropriate unit (e.g., abc) is used.Also,the function is non-case-sensitive tothe unit argument (e.g., should run properly with “Kg” or “kG” too).
2.Print the original input together with the converted value. Units must be printed in uppercase
Python code:
#defining function to convert between units
def convert(val,unit):
#checking if the unit is kg
if(unit.lower()=='kg'):
#printing the value and the converted value
print("{} KG = {} LB".format(val,val*2.205))
#checking if the unit is lb
elif(unit.lower()=='lb'):
#printing the value and the converted value
print("{} LB = {} KG".format(val,val/2.205))
else:
#printing Error
print("Error inappropriate unit")
#calling convert function for sample kg value
convert(10,'kG')
#calling convert function for sample lb value
convert(22.05,'lb')
#calling convert function for sample inappropriate value
convert(10,'abc')
Screenshot:
Input and Output: