Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
# Function to perform basic calculations
|
| 5 |
+
def calculator():
|
| 6 |
+
st.title("Enhanced Calculator")
|
| 7 |
+
|
| 8 |
+
# User input for numbers
|
| 9 |
+
num1 = st.number_input("Enter the first number", step=1.0)
|
| 10 |
+
num2 = st.number_input("Enter the second number", step=1.0)
|
| 11 |
+
|
| 12 |
+
# Dropdown for selecting the operation
|
| 13 |
+
operation = st.selectbox("Choose an operation", [
|
| 14 |
+
"Add", "Subtract", "Multiply", "Divide",
|
| 15 |
+
"Power (Exponentiation)", "Square Root (First Number)"
|
| 16 |
+
])
|
| 17 |
+
|
| 18 |
+
# Perform calculation based on the selected operation
|
| 19 |
+
if operation == "Add":
|
| 20 |
+
result = num1 + num2
|
| 21 |
+
elif operation == "Subtract":
|
| 22 |
+
result = num1 - num2
|
| 23 |
+
elif operation == "Multiply":
|
| 24 |
+
result = num1 * num2
|
| 25 |
+
elif operation == "Divide":
|
| 26 |
+
# Check to avoid division by zero
|
| 27 |
+
if num2 != 0:
|
| 28 |
+
result = num1 / num2
|
| 29 |
+
else:
|
| 30 |
+
result = "Error: Division by zero is not allowed"
|
| 31 |
+
elif operation == "Power (Exponentiation)":
|
| 32 |
+
result = num1 ** num2
|
| 33 |
+
elif operation == "Square Root (First Number)":
|
| 34 |
+
if num1 < 0:
|
| 35 |
+
result = "Error: Cannot compute square root of a negative number"
|
| 36 |
+
else:
|
| 37 |
+
result = math.sqrt(num1)
|
| 38 |
+
|
| 39 |
+
# Display the result
|
| 40 |
+
st.write("Result: ", result)
|
| 41 |
+
|
| 42 |
+
# Option to clear input values
|
| 43 |
+
clear_button = st.button("Clear")
|
| 44 |
+
if clear_button:
|
| 45 |
+
st.experimental_rerun()
|
| 46 |
+
|
| 47 |
+
# Call the calculator function
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
calculator()
|