File size: 1,493 Bytes
edbad2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import math

# Function to perform basic calculations
def calculator():
    st.title("Enhanced Calculator")

    # User input for numbers
    num1 = st.number_input("Enter the first number", step=1.0)
    num2 = st.number_input("Enter the second number", step=1.0)

    # Dropdown for selecting the operation
    operation = st.selectbox("Choose an operation", [
        "Add", "Subtract", "Multiply", "Divide", 
        "Power (Exponentiation)", "Square Root (First Number)"
    ])

    # Perform calculation based on the selected operation
    if operation == "Add":
        result = num1 + num2
    elif operation == "Subtract":
        result = num1 - num2
    elif operation == "Multiply":
        result = num1 * num2
    elif operation == "Divide":
        # Check to avoid division by zero
        if num2 != 0:
            result = num1 / num2
        else:
            result = "Error: Division by zero is not allowed"
    elif operation == "Power (Exponentiation)":
        result = num1 ** num2
    elif operation == "Square Root (First Number)":
        if num1 < 0:
            result = "Error: Cannot compute square root of a negative number"
        else:
            result = math.sqrt(num1)

    # Display the result
    st.write("Result: ", result)

    # Option to clear input values
    clear_button = st.button("Clear")
    if clear_button:
        st.experimental_rerun()

# Call the calculator function
if __name__ == "__main__":
    calculator()