Engineer786 commited on
Commit
f340dc9
·
verified ·
1 Parent(s): ccaaf3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -26
app.py CHANGED
@@ -1,34 +1,40 @@
1
  import streamlit as st
2
 
3
  def convert_temperature(value, from_unit, to_unit):
4
- """Converts temperature between Celsius and Fahrenheit."""
5
- if from_unit == "Celsius":
6
- if to_unit == "Fahrenheit":
7
- return (value * 9/5) + 32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  else:
9
- return value # No conversion needed for Celsius to Celsius
10
- elif from_unit == "Fahrenheit":
11
- if to_unit == "Celsius":
12
- return (value - 32) * 5/9
13
- else:
14
- return value # No conversion needed for Fahrenheit to Fahrenheit
15
- else:
16
- raise ValueError("Invalid temperature unit")
17
 
18
  st.title("Temperature Converter")
19
 
20
- # Input temperature value
21
- temperature_value = st.number_input("Enter temperature:", min_value=-273.15, step=0.1) # Allow negative values for Kelvin
22
-
23
- # Dropdown menus for unit selection
24
- unit_options = ["Celsius", "Fahrenheit"]
25
- from_unit = st.selectbox("From Unit:", options=unit_options)
26
- to_unit = st.selectbox("To Unit:", options=unit_options)
27
 
28
- # Convert button
29
- if st.button("Convert"):
30
- try:
31
- converted_value = convert_temperature(temperature_value, from_unit, to_unit)
32
- st.success(f"{temperature_value} {from_unit} is equal to {converted_value:.2f} {to_unit}") # Display result with 2 decimal places
33
- except ValueError as e:
34
- st.error(f"Error: {e}")
 
1
  import streamlit as st
2
 
3
  def convert_temperature(value, from_unit, to_unit):
4
+ """Converts temperature between Celsius, Fahrenheit, and Kelvin."""
5
+ if from_unit == "Celsius":
6
+ if to_unit == "Fahrenheit":
7
+ return (value * 9/5) + 32
8
+ elif to_unit == "Kelvin":
9
+ return value + 273.15
10
+ else:
11
+ return value # No conversion needed for Celsius to Celsius
12
+ elif from_unit == "Fahrenheit":
13
+ if to_unit == "Celsius":
14
+ return (value - 32) * 5/9
15
+ elif to_unit == "Kelvin":
16
+ return (value + 459.67) * 5/9
17
+ else:
18
+ return value # No conversion needed for Fahrenheit to Fahrenheit
19
+ elif from_unit == "Kelvin":
20
+ if to_unit == "Celsius":
21
+ return value - 273.15
22
+ elif to_unit == "Fahrenheit":
23
+ return (value * 9/5) - 459.67
24
+ else:
25
+ return value # No conversion needed for Kelvin to Kelvin
26
  else:
27
+ raise ValueError("Invalid temperature unit")
 
 
 
 
 
 
 
28
 
29
  st.title("Temperature Converter")
30
 
31
+ with st.container():
32
+ col1, col2 = st.columns(2)
33
+ with col1:
34
+ temperature_value = st.number_input("Enter temperature:", min_value=-273.15, step=0.1)
35
+ with col2:
36
+ from_unit = st.selectbox("From Unit:", options=["Celsius", "Fahrenheit", "Kelvin"])
37
+ to_unit = st.selectbox("To Unit:", options=["Celsius", "Fahrenheit", "Kelvin"])
38
 
39
+ converted_value = convert_temperature(temperature_value, from_unit, to_unit)
40
+ st.write(f"{temperature_value} {from_unit} is equal to {converted_value:.2f} {to_unit}")