Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,48 @@
|
|
| 1 |
# app.py
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
|
| 4 |
-
st.title("
|
| 5 |
|
| 6 |
# Input fields for numbers
|
| 7 |
num1 = st.number_input("Enter first number")
|
| 8 |
num2 = st.number_input("Enter second number")
|
|
|
|
| 9 |
|
| 10 |
# Operation selection
|
| 11 |
-
operation = st.selectbox(
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Calculation and display
|
| 14 |
if st.button("Calculate"):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
import streamlit as st
|
| 3 |
+
import math
|
| 4 |
|
| 5 |
+
st.title("Enhanced Calculator")
|
| 6 |
|
| 7 |
# Input fields for numbers
|
| 8 |
num1 = st.number_input("Enter first number")
|
| 9 |
num2 = st.number_input("Enter second number")
|
| 10 |
+
num3 = st.number_input("Enter third number")
|
| 11 |
|
| 12 |
# Operation selection
|
| 13 |
+
operation = st.selectbox(
|
| 14 |
+
"Select operation",
|
| 15 |
+
["+", "-", "*", "/", "Power", "Square Root", "Cube Root", "Average"],
|
| 16 |
+
)
|
| 17 |
|
| 18 |
# Calculation and display
|
| 19 |
if st.button("Calculate"):
|
| 20 |
+
try: # Wrap in a try-except block for better error handling
|
| 21 |
+
if operation == "+":
|
| 22 |
+
result = num1 + num2 + num3
|
| 23 |
+
elif operation == "-":
|
| 24 |
+
result = num1 - num2 - num3
|
| 25 |
+
elif operation == "*":
|
| 26 |
+
result = num1 * num2 * num3
|
| 27 |
+
elif operation == "/":
|
| 28 |
+
if num2 == 0 or num3 == 0:
|
| 29 |
+
raise ZeroDivisionError("Division by zero is not allowed.") # Raise exception
|
| 30 |
+
else:
|
| 31 |
+
result = num1 / num2 / num3
|
| 32 |
+
elif operation == "Power":
|
| 33 |
+
result = num1**num2
|
| 34 |
+
elif operation == "Square Root":
|
| 35 |
+
if num1 < 0:
|
| 36 |
+
raise ValueError("Square root of a negative number is not allowed.") # Raise exception
|
| 37 |
+
else:
|
| 38 |
+
result = math.sqrt(num1)
|
| 39 |
+
elif operation == "Cube Root":
|
| 40 |
+
result = num1**(1/3)
|
| 41 |
+
elif operation == "Average":
|
| 42 |
+
result = (num1 + num2 + num3) / 3
|
| 43 |
+
st.write(f"Result: {result}")
|
| 44 |
+
|
| 45 |
+
except (ZeroDivisionError, ValueError) as e: # Catch specific exceptions
|
| 46 |
+
st.error(str(e)) # Display the error message to the user
|
| 47 |
+
except Exception as e: # Catch any other unexpected exceptions
|
| 48 |
+
st.error(f"An error occurred: {e}") # Display a generic error message
|