Answer to Question #210969 in Python for ravi

Question #210969

You are given the temperature T of an object in one of Celsius, Fahrenheit, and Kelvin scales.

Write a program to print T in all scales viz Celsius, Fahrenheit, and Kelvin.

Formula to convert from Fahrenheit F to Celsius C is C = (F - 32) * 5 / 9.

Formula to convert from Kelvin K to Celsius C is C = K - 273.

Here "C", "F", "K" represent that the temperature scale is in Celsius, Fahrenheit and Kelvin scales respectively.

The input contains the temperature (a number) and the unit of the temperature scale (C, F, K) without any space.

The output contains temperature in Celsius, Fahrenheit and Kelvin scales in each line in the format similar to input and the value of the temperature is rounded to 2 decimal places.

For example, if the given temperature Value is 25C then Celsius value is 25.0C, Fahrenheit value is 77.0F, and Kelvin value is 298.0K.


1
Expert's answer
2021-06-27T14:11:14-0400
temperature = input()
celsius, fahrenheit, kelvin = 0, 0, 0
if temperature[-1].lower() == 'c':
    celsius = float(temperature[:-1])
    fahrenheit = celsius * 9 / 5 + 32
    kelvin = celsius + 273
elif temperature[-1].lower() == 'f':
    fahrenheit = float(temperature[:-1])
    celsius = (fahrenheit - 32) * 5 / 9.
    kelvin = celsius + 273
elif temperature[-1].lower() == 'k':
    kelvin = float(temperature[:-1])
    celsius = kelvin - 273
    fahrenheit = celsius * 9 / 5 + 32
print(f"Celsius value is {round(celsius, 2)}C")
print(f"Fahrenheit value is {round(fahrenheit, 2)}F")
print(f"Kelvin value is {round(kelvin, 2)}K")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS