Back to: Python Programming
0
This exercise practises using user input and converting units to something else.
Write code that asks a user for a weight and whether they want to convert kilograms (kg) to pounds (lb) or pounds to kilograms. (There are 2.2062 pounds in 1 Kg). Round the answer to 2 decimal places.
weight = float(input("Enter the weight of your item: "))
unit = input("Enter the unit of your item (K or P): ")
if unit == "K":
result = round(weight * 2.2062, 2)
print(f"A {weight}kg item weighs {result} pounds.")
elif unit == "P":
result = round(weight / 2.2062, 2)
print(f"A {weight}lb item weighs {result} kgs.")
else:
print("Sorry, I can only convert between kilograms <--> pounds.")
Exercise:
Write code to convert the temperature provided by a user from Fahrenheit to Celsius or Celsius to Fahrenheit. Round your answer to 1 decimal place.
[note: °F = °C × (9/5) + 32 and °C = (°F – 32) × (5/9) ]