ahsangahothi's picture
Update app.py
ea2f947 verified
import streamlit as st
def convert_temperature(value, from_unit, to_unit):
"""Convert 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
elif from_unit == "Fahrenheit":
if to_unit == "Celsius":
return (value - 32) * 5/9
elif to_unit == "Kelvin":
return (value - 32) * 5/9 + 273.15
elif from_unit == "Kelvin":
if to_unit == "Celsius":
return value - 273.15
elif to_unit == "Fahrenheit":
return (value - 273.15) * 9/5 + 32
def main():
st.title("Temperature Converter")
# User input: Temperature value
value = st.number_input("Enter temperature value:", min_value=-273.15)
# User input: Temperature units
from_unit = st.selectbox("Convert from:", ["Celsius", "Fahrenheit", "Kelvin"])
to_unit = st.selectbox("Convert to:", ["Celsius", "Fahrenheit", "Kelvin"])
# Convert and display result
if from_unit != to_unit:
converted_value = convert_temperature(value, from_unit, to_unit)
st.write(f"{value} {from_unit} is equal to {converted_value} {to_unit}.")
else:
st.write("Please choose different units to convert.")
if __name__ == "__main__":
main()