SimpleCalulator / app.py
JaveriaZia's picture
Create app.py
09bc469 verified
import streamlit as st
import math
# Custom styling using Markdown
st.markdown(
"""
<style>
.main {
background-color: #f0f4f8;
}
.stButton>button {
color: white;
background-color: #4CAF50;
font-size: 16px;
border-radius: 10px;
padding: 10px 24px;
}
.stSelectbox, .stNumberInput {
background-color: #ffffff;
}
</style>
""",
unsafe_allow_html=True
)
# Title
st.markdown("<h1 style='color: #3366cc; text-align: center;'>Advanced Calculator</h1>", unsafe_allow_html=True)
# Input fields
num1 = st.number_input("Enter First Number", format="%.4f")
num2 = st.number_input("Enter Second Number", format="%.4f")
# Operation choices
operations = [
"Add", "Subtract", "Multiply", "Divide",
"Power", "Modulus", "Square Root (1st Number)", "Logarithm (1st Number)"
]
operation = st.selectbox("Choose Operation", operations)
# Calculate button
if st.button("Calculate"):
try:
if operation == "Add":
result = num1 + num2
elif operation == "Subtract":
result = num1 - num2
elif operation == "Multiply":
result = num1 * num2
elif operation == "Divide":
if num2 != 0:
result = num1 / num2
else:
st.error("❌ Cannot divide by zero!")
result = None
elif operation == "Power":
result = num1 ** num2
elif operation == "Modulus":
result = num1 % num2
elif operation == "Square Root (1st Number)":
if num1 >= 0:
result = math.sqrt(num1)
else:
st.error("❌ Cannot take square root of a negative number!")
result = None
elif operation == "Logarithm (1st Number)":
if num1 > 0:
result = math.log(num1)
else:
st.error("❌ Logarithm undefined for 0 or negative numbers!")
result = None
if result is not None:
st.success(f"βœ… Result: {result:.4f}")
except Exception as e:
st.error(f"⚠️ Error: {str(e)}")