File size: 1,428 Bytes
8ff8d52
 
a456aff
 
 
 
 
01f307a
 
 
 
a456aff
01f307a
 
 
a456aff
01f307a
 
 
 
 
 
 
a456aff
01f307a
 
 
a456aff
01f307a
 
 
 
 
 
 
a456aff
01f307a
a456aff
 
 
 
 
 
 
 
 
 
01f307a
 
 
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
41
42
43
44
45
46
47
48
49
50
51
import streamlit as st

# Title and description
st.set_page_config(page_title="Temperature Converter", layout="centered")
st.title("🌡️ Temperature Converter")

# Conversion function
def convert_temperature(value, from_unit, to_unit):
    if from_unit == to_unit:
        return value

    # Convert to Celsius first
    if from_unit == "Celsius":
        celsius = value
    elif from_unit == "Fahrenheit":
        celsius = (value - 32) * 5 / 9
    elif from_unit == "Kelvin":
        celsius = value - 273.15

    # Convert from Celsius to target
    if to_unit == "Celsius":
        return celsius
    elif to_unit == "Fahrenheit":
        return (celsius * 9 / 5) + 32
    elif to_unit == "Kelvin":
        return celsius + 273.15

# Input section
col1, col2 = st.columns(2)

with col1:
    from_unit = st.selectbox("From Unit", ["Celsius", "Fahrenheit", "Kelvin"])
with col2:
    to_unit = st.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"])

temp_input = st.text_input("Enter Temperature (0 - 500)", "0")

# Validate input
try:
    temp_value = float(temp_input)
    if temp_value < 0 or temp_value > 500:
        st.error("Please enter a temperature between 0 and 500.")
    else:
        converted = convert_temperature(temp_value, from_unit, to_unit)
        st.success(f"Converted Temperature: {converted:.2f} {to_unit}")
except ValueError:
    st.warning("Please enter a valid numeric temperature.")