File size: 1,558 Bytes
372fe47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import math

st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")

st.title("🧮 Advanced Calculator")

# Select operation
operation = st.selectbox(
    "Select Operation",
    (
        "Add",
        "Subtract",
        "Multiply",
        "Divide",
        "Square",
        "Square Root",
        "Power (x^y)",
        "Log (base 10)"
    )
)

# Input fields
if operation in ["Add", "Subtract", "Multiply", "Divide", "Power (x^y)"]:
    num1 = st.number_input("Enter first number")
    num2 = st.number_input("Enter second number")

elif operation in ["Square", "Square Root", "Log (base 10)"]:
    num1 = st.number_input("Enter number")

# Calculate button
if st.button("Calculate"):
    try:
        if operation == "Add":
            result = num1 + num2

        elif operation == "Subtract":
            result = num1 - num2

        elif operation == "Multiply":
            result = num1 * num2

        elif operation == "Divide":
            result = "Error! Division by zero." if num2 == 0 else num1 / num2

        elif operation == "Square":
            result = num1 ** 2

        elif operation == "Square Root":
            result = "Error! Negative number." if num1 < 0 else math.sqrt(num1)

        elif operation == "Power (x^y)":
            result = num1 ** num2

        elif operation == "Log (base 10)":
            result = "Error! Input must be > 0." if num1 <= 0 else math.log10(num1)

        st.success(f"Result: {result}")

    except Exception as e:
        st.error(f"Error: {e}")