# app.py import streamlit as st import math st.set_page_config(page_title="Enhanced Calculator", page_icon=":calculator:", layout="wide") # Custom CSS for a more professional look st.markdown( """ """, unsafe_allow_html=True, ) st.title("Enhanced Calculator") with st.container() as calculator_container: # Enclose in a container for styling col1, col2 = st.columns([1, 1]) # Two columns for inputs and operations with col1: st.subheader("Input Numbers") num1 = st.number_input("First Number", value=0.0) num2 = st.number_input("Second Number", value=0.0) num3 = st.number_input("Third Number", value=0.0) with col2: st.subheader("Select Operation") operation = st.selectbox( "Operation", [ "+", "-", "*", "/", "Power", "Square Root", "Cube Root", "Average", "Logarithm", # Added Logarithm "Sin", "Cos", "Tan", # Added Trigonometric functions "Absolute Value", #Added Absolute Value function "Factorial" #Added Factorial function ], ) if st.button("Calculate"): try: if operation == "+": result = num1 + num2 + num3 elif operation == "-": result = num1 - num2 - num3 elif operation == "*": result = num1 * num2 * num3 elif operation == "/": if num2 == 0 or num3 == 0: raise ZeroDivisionError("Division by zero is not allowed.") else: result = num1 / num2 / num3 elif operation == "Power": result = num1**num2 elif operation == "Square Root": if num1 < 0: raise ValueError("Square root of a negative number is not allowed.") else: result = math.sqrt(num1) elif operation == "Cube Root": result = num1**(1/3) elif operation == "Average": result = (num1 + num2 + num3) / 3 elif operation == "Logarithm": if num1 <= 0: raise ValueError("Logarithm of a non-positive number is not allowed.") else: result = math.log(num1) elif operation == "Sin": result = math.sin(math.radians(num1)) # Convert to radians elif operation == "Cos": result = math.cos(math.radians(num1)) # Convert to radians elif operation == "Tan": result = math.tan(math.radians(num1)) # Convert to radians elif operation == "Absolute Value": result = abs(num1) elif operation == "Factorial": if num1 < 0 or not num1.is_integer(): raise ValueError("Factorial is only defined for non-negative integers.") else: result = math.factorial(int(num1)) st.markdown(f"
Result: {result}
", unsafe_allow_html=True) except (ZeroDivisionError, ValueError) as e: st.markdown(f"
{str(e)}
", unsafe_allow_html=True) except Exception as e: st.markdown(f"
An error occurred: {e}
", unsafe_allow_html=True) st.markdown("---") st.write("Made with Streamlit")