Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
# App title
|
| 5 |
+
st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
|
| 6 |
+
st.title("🧮 Advanced Calculator")
|
| 7 |
+
st.write("An advanced calculator with percentage & history")
|
| 8 |
+
|
| 9 |
+
# Initialize history in session state
|
| 10 |
+
if "history" not in st.session_state:
|
| 11 |
+
st.session_state.history = []
|
| 12 |
+
|
| 13 |
+
# Select operation
|
| 14 |
+
operation = st.selectbox(
|
| 15 |
+
"Choose an operation",
|
| 16 |
+
[
|
| 17 |
+
"Addition (+)",
|
| 18 |
+
"Subtraction (-)",
|
| 19 |
+
"Multiplication (*)",
|
| 20 |
+
"Division (/)",
|
| 21 |
+
"Power (x^y)",
|
| 22 |
+
"Modulus (%)",
|
| 23 |
+
"Square Root (√x)",
|
| 24 |
+
"Percentage (x % of y)"
|
| 25 |
+
]
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Inputs
|
| 29 |
+
if operation == "Square Root (√x)":
|
| 30 |
+
num = st.number_input("Enter number", value=0.0)
|
| 31 |
+
|
| 32 |
+
elif operation == "Percentage (x % of y)":
|
| 33 |
+
x = st.number_input("Enter percentage value", value=0.0)
|
| 34 |
+
y = st.number_input("Enter total value", value=0.0)
|
| 35 |
+
|
| 36 |
+
else:
|
| 37 |
+
num1 = st.number_input("Enter first number", value=0.0)
|
| 38 |
+
num2 = st.number_input("Enter second number", value=0.0)
|
| 39 |
+
|
| 40 |
+
# Calculate button
|
| 41 |
+
if st.button("Calculate"):
|
| 42 |
+
try:
|
| 43 |
+
if operation == "Addition (+)":
|
| 44 |
+
result = num1 + num2
|
| 45 |
+
record = f"{num1} + {num2} = {result}"
|
| 46 |
+
|
| 47 |
+
elif operation == "Subtraction (-)":
|
| 48 |
+
result = num1 - num2
|
| 49 |
+
record = f"{num1} - {num2} = {result}"
|
| 50 |
+
|
| 51 |
+
elif operation == "Multiplication (*)":
|
| 52 |
+
result = num1 * num2
|
| 53 |
+
record = f"{num1} * {num2} = {result}"
|
| 54 |
+
|
| 55 |
+
elif operation == "Division (/)":
|
| 56 |
+
if num2 == 0:
|
| 57 |
+
st.error("Division by zero is not allowed")
|
| 58 |
+
st.stop()
|
| 59 |
+
result = num1 / num2
|
| 60 |
+
record = f"{num1} / {num2} = {result}"
|
| 61 |
+
|
| 62 |
+
elif operation == "Power (x^y)":
|
| 63 |
+
result = num1 ** num2
|
| 64 |
+
record = f"{num1} ^ {num2} = {result}"
|
| 65 |
+
|
| 66 |
+
elif operation == "Modulus (%)":
|
| 67 |
+
result = num1 % num2
|
| 68 |
+
record = f"{num1} % {num2} = {result}"
|
| 69 |
+
|
| 70 |
+
elif operation == "Square Root (√x)":
|
| 71 |
+
if num < 0:
|
| 72 |
+
st.error("Cannot calculate square root of negative number")
|
| 73 |
+
st.stop()
|
| 74 |
+
result = math.sqrt(num)
|
| 75 |
+
record = f"√{num} = {result}"
|
| 76 |
+
|
| 77 |
+
elif operation == "Percentage (x % of y)":
|
| 78 |
+
result = (x / 100) * y
|
| 79 |
+
record = f"{x}% of {y} = {result}"
|
| 80 |
+
|
| 81 |
+
# Display result
|
| 82 |
+
st.success(f"Result: {result}")
|
| 83 |
+
|
| 84 |
+
# Save history
|
| 85 |
+
st.session_state.history.append(record)
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
st.error(f"Error: {e}")
|
| 89 |
+
|
| 90 |
+
# Show history
|
| 91 |
+
st.subheader("📜 Calculation History")
|
| 92 |
+
if st.session_state.history:
|
| 93 |
+
for h in st.session_state.history:
|
| 94 |
+
st.write(h)
|
| 95 |
+
else:
|
| 96 |
+
st.info("No calculations yet")
|