Engineer786's picture
Update app.py
f340dc9 verified
import streamlit as st
def convert_temperature(value, from_unit, to_unit):
"""Converts temperature between Celsius, Fahrenheit, and Kelvin."""
if from_unit == "Celsius":
if to_unit == "Fahrenheit":
return (value * 9/5) + 32
elif to_unit == "Kelvin":
return value + 273.15
else:
return value # No conversion needed for Celsius to Celsius
elif from_unit == "Fahrenheit":
if to_unit == "Celsius":
return (value - 32) * 5/9
elif to_unit == "Kelvin":
return (value + 459.67) * 5/9
else:
return value # No conversion needed for Fahrenheit to Fahrenheit
elif from_unit == "Kelvin":
if to_unit == "Celsius":
return value - 273.15
elif to_unit == "Fahrenheit":
return (value * 9/5) - 459.67
else:
return value # No conversion needed for Kelvin to Kelvin
else:
raise ValueError("Invalid temperature unit")
st.title("Temperature Converter")
with st.container():
col1, col2 = st.columns(2)
with col1:
temperature_value = st.number_input("Enter temperature:", min_value=-273.15, step=0.1)
with col2:
from_unit = st.selectbox("From Unit:", options=["Celsius", "Fahrenheit", "Kelvin"])
to_unit = st.selectbox("To Unit:", options=["Celsius", "Fahrenheit", "Kelvin"])
converted_value = convert_temperature(temperature_value, from_unit, to_unit)
st.write(f"{temperature_value} {from_unit} is equal to {converted_value:.2f} {to_unit}")