Temperature Conversion
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.
when I am using above question code i was unable to get units for output.
Foe example Expected output is
25.0C
77.0F
298.0K
But I am getting as
25
77.0
298
please explain the errors in the code
s = input("Temperature and units: ")
unit = s[-1]
number=float(s[0:-1])
def F_to_C(n):
return (n-32)*5/9
def C_to_F(n):
return n*9./5+32.
def C_to_K(n):
return n+273.
def K_to_C(n):
return n-273.
def F_to_K(n):
return C_to_K(F_to_C(n))
def K_to_F(n):
return(C_to_F(K_to_C(n)))
if unit=='F':
print(str(number)+'F\n')
print(str(F_to_C(number))+'C\n')
print(str(F_to_K(number))+'K\n')
elif unit=='C':
print(str(number)+'C\n')
print(str(C_to_F(number))+'F\n')
print(str(C_to_K(number))+'K\n')
elif unit=='K':
print(str(number)+'K\n')
print(str(K_to_F(number))+'F\n')
print(str(K_to_C(number))+'C\n')
else:
print("Error: This temperatute unit does not exist!")
Comments
Leave a comment