File size: 1,567 Bytes
d3cd1e9
 
 
f340dc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3cd1e9
f340dc9
d3cd1e9
 
 
f340dc9
 
 
 
 
 
 
d3cd1e9
f340dc9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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}")