Calculator / app.py
BQGamer's picture
Update app.py
bd38b70 verified
import streamlit as st
import math
# Title of the application
st.title('Advanced Calculator')
# Sidebar for choosing operation
operation = st.sidebar.selectbox(
"Choose operation",
("Basic", "Advanced", "Trigonometric", "Logarithmic", "Factorial", "Percentage", "Memory")
)
# User input for numbers
num1 = st.number_input('Enter first number', value=0.0)
num2 = st.number_input('Enter second number (optional)', value=0.0)
# Memory variable (to store previous result)
if 'memory' not in st.session_state:
st.session_state.memory = 0.0
# Basic Operations
if operation == "Basic":
basic_op = st.selectbox("Choose basic operation", ("Add", "Subtract", "Multiply", "Divide"))
if basic_op == "Add":
result = num1 + num2
elif basic_op == "Subtract":
result = num1 - num2
elif basic_op == "Multiply":
result = num1 * num2
elif basic_op == "Divide":
if num2 != 0:
result = num1 / num2
else:
result = "Error: Division by Zero"
# Advanced Operations
elif operation == "Advanced":
advanced_op = st.selectbox("Choose advanced operation", ("Power", "Square Root"))
if advanced_op == "Power":
result = num1 ** num2
elif advanced_op == "Square Root":
if num1 >= 0:
result = math.sqrt(num1)
else:
result = "Error: Cannot take square root of negative number"
# Trigonometric Operations
elif operation == "Trigonometric":
trig_op = st.selectbox("Choose trigonometric function", ("Sine", "Cosine", "Tangent"))
angle_in_degrees = st.checkbox("Use degrees instead of radians?")
if trig_op == "Sine":
if angle_in_degrees:
result = math.sin(math.radians(num1))
else:
result = math.sin(num1)
elif trig_op == "Cosine":
if angle_in_degrees:
result = math.cos(math.radians(num1))
else:
result = math.cos(num1)
elif trig_op == "Tangent":
if angle_in_degrees:
result = math.tan(math.radians(num1))
else:
result = math.tan(num1)
# Logarithmic Operations
elif operation == "Logarithmic":
log_op = st.selectbox("Choose logarithmic operation", ("Log Base 10", "Natural Log"))
if log_op == "Log Base 10":
if num1 > 0:
result = math.log10(num1)
else:
result = "Error: Logarithm only defined for positive numbers"
elif log_op == "Natural Log":
if num1 > 0:
result = math.log(num1)
else:
result = "Error: Logarithm only defined for positive numbers"
# Factorial
elif operation == "Factorial":
if num1 >= 0 and num1 == int(num1):
result = math.factorial(int(num1))
else:
result = "Error: Factorial is only defined for non-negative integers"
# Percentage Calculation
elif operation == "Percentage":
percentage_op = st.selectbox("Choose percentage operation", ("Find Percentage", "Find Percentage of"))
if percentage_op == "Find Percentage":
result = (num1 * num2) / 100
elif percentage_op == "Find Percentage of":
result = (num1 * 100) / num2
# Memory Feature
elif operation == "Memory":
memory_op = st.selectbox("Choose memory operation", ("Store Result", "Recall Result", "Clear Memory"))
if memory_op == "Store Result":
st.session_state.memory = result
result = f"Result stored in memory: {st.session_state.memory}"
elif memory_op == "Recall Result":
result = f"Memory contains: {st.session_state.memory}"
elif memory_op == "Clear Memory":
st.session_state.memory = 0.0
result = "Memory cleared"
# Display the result
st.write(f"Result: {result}")