Basic Temperature Conversion Program
This program takes a temperature input from the user, identifies the scale (Celsius, Fahrenheit, Kelvin), and converts it to the other two scales. Let’s break down the code step-by-step.
For higher accuracy in the Kelvin scale use 273.15 instead of 273
x = str(input("Enter in which scale you measured temperature (Celsius, Fahrenheit, Kelvin)? Enter C or F or K: "))
# 160 is 5*32 .... equations are simplified
if x=="C" or x=="c":
c=eval(input("Enter your temperature in Celsius scale: "))
to_fahrenheit=(9*c+160)/5
to_kelvin=c+273
print("Celsius :", c, "\nFahrenheit :", to_fahrenheit, "\nKelvin: ", to_kelvin)
elif x=="F" or x=="f":
f=eval(input("Enter your temperature in Fahrenheit scale: "))
to_celsius=(5*f-160)/9
to_kelvin=((5*f-160)/9)+273
print("Celsius :", to_celsius, "\nFahrenheit :", f, "\nKelvin: ", to_kelvin)
elif x== "K" or x=="k":
k=eval(input("Enter your temperature in Kalvin scale: "))
to_celsius=k-273
to_fahrenheit=(9*(k-273)+160)/5
print("Celsius :", to_celsius, "\nFahrenheit :", to_fahrenheit, "\nKelvin: ", k)
Explanation
>Firstly the this code takes input from user in which scale user measured the temperature
>Then user asked for another input in which scale they want to convert
- The user is prompted to enter the temperature scale: ‘C’ for Celsius, ‘F’ for Fahrenheit, or ‘K’ for Kelvin.
- Conversion Calculations:
- Depending on the input scale, the program calculates the temperature in the other two scales using the following formulas:
Celsius to Fahrenheit:
\[ F = \frac{9}{5}C + 32 \]
Celsius to Kelvin:
\[ K = C + 273 \]
Fahrenheit to Celsius:
\[ C = \frac{5}{9}(F – 32) \]
Fahrenheit to Kelvin:
\[ K = \frac{5}{9}(F – 32) + 273 \]
Kelvin to Celsius:
\[ C = K – 273 \]
Kelvin to Fahrenheit:
\[ F = \frac{9}{5}(K – 273) + 32 \]
Complete code with some added feature
x = str(input("Enter in which scale you measured temperature (Celsius, Fahrenheit, Kelvin)? Enter C or F or K: "))
# 160 is 5*32 .... equations are simplified
if x == "C" or x == "c":
c = eval(input("Enter your temperature in Celsius scale: "))
to_fahrenheit = (9 * c + 160) / 5
to_kelvin = c + 273
while to_kelvin < 0:
print("Temperature cannot be less than absolute zero")
c = eval(input("Enter your temperature in Celsius scale: "))
to_fahrenheit = (9 * c + 160) / 5
to_kelvin = c + 273
print("Celsius:", c, "\nFahrenheit:", to_fahrenheit, "\nKelvin:", to_kelvin)
elif x == "F" or x == "f":
f = eval(input("Enter your temperature in Fahrenheit scale: "))
to_celsius = (5 * f - 160) / 9
to_kelvin = ((5 * f - 160) / 9) + 273
while to_kelvin < 0:
print("Temperature cannot be less than absolute zero")
f = eval(input("Enter your temperature in Fahrenheit scale: "))
to_celsius = (5 * f - 160) / 9
to_kelvin = ((5 * f - 160) / 9) + 273
print("Celsius:", to_celsius, "\nFahrenheit:", f, "\nKelvin:", to_kelvin)
elif x == "K" or x == "k":
k = eval(input("Enter your temperature in Kelvin scale: "))
while k < 0:
print("Temperature cannot be less than absolute zero")
k = eval(input("Enter your temperature in Kelvin scale: "))
to_celsius = k - 273
to_fahrenheit = (9 * (k - 273) + 160) / 5
print("Celsius:", to_celsius, "\nFahrenheit:", to_fahrenheit, "\nKelvin:", k)