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.
def main():
#The input contains the temperature (a number) and the
#unit of the temperature scale (C, F, K) without any space.
temperatureScale=input("Enter the temperature and scale (C, F or K) (example - 22.5F): ")
temperature=float(temperatureScale[0:-1])
scale=temperatureScale[-1]
#set temperature
temperatureCelsius=temperature
temperatureFahrenheit=temperature
temperatureKelvin=temperature
isCorrect=True
if scale.upper()=='C':
temperatureFahrenheit=(temperatureCelsius * 1.8) + 32
temperatureKelvin=temperatureCelsius+273.15
elif scale.upper()=='F':
#Formula to convert from Fahrenheit F to Celsius C is C = (F - 32) * 5 / 9.
temperatureCelsius=(temperatureFahrenheit - 32) * 5.0 / 9.0
temperatureKelvin=(temperatureFahrenheit - 32) * 5/9 + 273.15
elif scale.upper()=='K':
#Formula to convert from Kelvin K to Celsius C is C = K - 273.
temperatureCelsius= temperatureKelvin - 273.15
temperatureFahrenheit=(temperatureKelvin - 273.15) * 9/5 + 32
else:
print("\nInvalid scale!!!\n")
isCorrect=False
if isCorrect:
#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.
print("\nThe temperature in Celsius = {:.2f}".format(temperatureCelsius))
print("The temperature in Fahrenheit = {:.2f}".format(temperatureFahrenheit))
print("The temperature in Kelvin = {:.2f}".format(temperatureKelvin))
main()
Example:
Enter the temperature and scale (C, F or K) (example - 22.5F): 100c
The temperature in Celsius = 100.00
The temperature in Fahrenheit = 212.00
The temperature in Kelvin = 373.15
>>>
Enter the temperature and scale (C, F or K) (example - 22.5F): 100F
The temperature in Celsius = 37.78
The temperature in Fahrenheit = 100.00
The temperature in Kelvin = 310.93
>>>
Enter the temperature and scale (C, F or K) (example - 22.5F): -100K
The temperature in Celsius = -373.15
The temperature in Fahrenheit = -639.67
The temperature in Kelvin = -100.00
>>>
Enter the temperature and scale (C, F or K) (example - 22.5F): 100G
Invalid scale!!!
Comments
Leave a comment