File size: 2,290 Bytes
e38d5ce
2a088e2
e38d5ce
 
2a088e2
e38d5ce
2a088e2
 
e38d5ce
2a088e2
e38d5ce
 
 
2a088e2
e38d5ce
2a088e2
e38d5ce
2a088e2
 
 
 
 
e38d5ce
 
 
 
 
2a088e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e38d5ce
 
2a088e2
 
 
 
 
 
 
 
 
 
 
e38d5ce
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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()