Maira-ghaffar's picture
Update app.py
4312613 verified
import streamlit as st
import math
st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
st.title("🧮 Advanced Calculator")
st.write("A simple advanced calculator built with Streamlit and deployed on Hugging Face.")
# Initialize history in session state
if "history" not in st.session_state:
st.session_state.history = []
# User inputs
num1 = st.number_input("Enter first number", value=0.0, step=1.0)
num2 = st.number_input("Enter second number", value=0.0, step=1.0)
operation = st.selectbox(
"Choose operation",
["Addition (+)", "Subtraction (-)", "Multiplication (*)", "Division (/)", "Power (a^b)", "Modulus (%)"]
)
# Calculate button
if st.button("Calculate"):
if operation == "Addition (+)":
result = num1 + num2
record = f"{num1} + {num2} = {result}"
elif operation == "Subtraction (-)":
result = num1 - num2
record = f"{num1} - {num2} = {result}"
elif operation == "Multiplication (*)":
result = num1 * num2
record = f"{num1} * {num2} = {result}"
elif operation == "Division (/)":
if num2 == 0:
st.error("❌ Division by zero is not allowed.")
result = None
record = None
else:
result = num1 / num2
record = f"{num1} / {num2} = {result}"
elif operation == "Power (a^b)":
result = num1 ** num2
record = f"{num1} ^ {num2} = {result}"
elif operation == "Modulus (%)":
if num2 == 0:
st.error("❌ Modulus by zero is not allowed.")
result = None
record = None
else:
result = num1 % num2
record = f"{num1} % {num2} = {result}"
if result is not None:
st.success(f"✅ Result: {result}")
st.session_state.history.append(record)
# Show history
st.subheader("📜 Calculation History")
if len(st.session_state.history) == 0:
st.info("No calculations yet.")
else:
for item in st.session_state.history[::-1]:
st.write("•", item)
# Clear history button
if st.button("Clear History"):
st.session_state.history = []
st.success("History cleared successfully!")
st.markdown("---")
st.caption("Built with ❤️ using Python & Streamlit")