AamerAkhter's picture
Update app.py
2a088e2 verified
Raw
History Blame Contribute Delete
2.29 kB
import streamlit as st
import math
def main():
st.set_page_config(page_title="Advanced Calculator", page_icon="🔢")
st.title("🧮 Math Mastery Calculator")
st.write("Perform basic and advanced arithmetic operations.")
# Layout: Two columns for numbers
col1, col2 = st.columns(2)
with col1:
num1 = st.number_input("Enter first number (x)", value=0.0)
with col2:
num2 = st.number_input("Enter second number (y)", value=0.0)
# Expanded operator selection
operation = st.selectbox(
"Choose an operation",
("+", "-", "*", "/", "** (Power)", "% (Modulo)", "√ (Square Root of x)")
)
result = None
error_msg = None
if st.button("Calculate"):
try:
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
if num2 != 0:
result = num1 / num2
else:
error_msg = "Division Error: You can't divide by zero!"
elif operation == "** (Power)":
result = math.pow(num1, num2)
elif operation == "% (Modulo)":
result = num1 % num2
elif operation == "√ (Square Root of x)":
if num1 >= 0:
result = math.sqrt(num1)
else:
error_msg = "Math Error: Cannot calculate square root of a negative number!"
except Exception as e:
error_msg = f"An error occurred: {e}"
# Display results with nice formatting
if error_msg:
st.error(error_msg)
elif result is not None:
st.balloons() # Just for a bit of fun on success
st.success(f"### Result: {result}")
# Adding a little tip section
with st.expander("Operation Help"):
st.write("""
* **Power**: Raises the first number to the power of the second ($x^y$).
* **Modulo**: Returns the remainder after division.
* **Square Root**: Only calculates the root of the **first** number provided.
""")
if __name__ == "__main__":
main()