Kamran Zulfiqar commited on
Commit
a456aff
·
verified ·
1 Parent(s): f37da20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -1,15 +1,19 @@
1
  import streamlit as st
2
 
3
- # Temperature conversion functions
 
 
 
 
4
  def convert_temperature(value, from_unit, to_unit):
5
  if from_unit == to_unit:
6
  return value
7
 
8
- # Convert input to Celsius
9
  if from_unit == "Celsius":
10
  celsius = value
11
  elif from_unit == "Fahrenheit":
12
- celsius = (value - 32) * 5/9
13
  elif from_unit == "Kelvin":
14
  celsius = value - 273.15
15
 
@@ -17,15 +21,11 @@ def convert_temperature(value, from_unit, to_unit):
17
  if to_unit == "Celsius":
18
  return celsius
19
  elif to_unit == "Fahrenheit":
20
- return (celsius * 9/5) + 32
21
  elif to_unit == "Kelvin":
22
  return celsius + 273.15
23
 
24
- # Streamlit UI
25
- st.set_page_config(page_title="Temperature Converter", layout="centered")
26
-
27
- st.title("🌡️ Temperature Converter")
28
-
29
  col1, col2 = st.columns(2)
30
 
31
  with col1:
@@ -33,10 +33,18 @@ with col1:
33
  with col2:
34
  to_unit = st.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"])
35
 
36
- temp_value = st.slider("Select Temperature", min_value=0.0, max_value=500.0, step=0.1)
37
 
38
- converted = convert_temperature(temp_value, from_unit, to_unit)
 
 
 
 
 
 
 
 
 
39
 
40
- st.markdown(f"### 🔁 Converted Temperature: **{converted:.2f} {to_unit}**")
41
 
42
 
 
1
  import streamlit as st
2
 
3
+ # Title and description
4
+ st.set_page_config(page_title="Temperature Converter", layout="centered")
5
+ st.title("🌡️ Temperature Converter")
6
+
7
+ # Conversion function
8
  def convert_temperature(value, from_unit, to_unit):
9
  if from_unit == to_unit:
10
  return value
11
 
12
+ # Convert to Celsius first
13
  if from_unit == "Celsius":
14
  celsius = value
15
  elif from_unit == "Fahrenheit":
16
+ celsius = (value - 32) * 5 / 9
17
  elif from_unit == "Kelvin":
18
  celsius = value - 273.15
19
 
 
21
  if to_unit == "Celsius":
22
  return celsius
23
  elif to_unit == "Fahrenheit":
24
+ return (celsius * 9 / 5) + 32
25
  elif to_unit == "Kelvin":
26
  return celsius + 273.15
27
 
28
+ # Input section
 
 
 
 
29
  col1, col2 = st.columns(2)
30
 
31
  with col1:
 
33
  with col2:
34
  to_unit = st.selectbox("To Unit", ["Celsius", "Fahrenheit", "Kelvin"])
35
 
36
+ temp_input = st.text_input("Enter Temperature (0 - 500)", "0")
37
 
38
+ # Validate input
39
+ try:
40
+ temp_value = float(temp_input)
41
+ if temp_value < 0 or temp_value > 500:
42
+ st.error("Please enter a temperature between 0 and 500.")
43
+ else:
44
+ converted = convert_temperature(temp_value, from_unit, to_unit)
45
+ st.success(f"Converted Temperature: {converted:.2f} {to_unit}")
46
+ except ValueError:
47
+ st.warning("Please enter a valid numeric temperature.")
48
 
 
49
 
50