Calculator / app.py
Tanseer45203's picture
Create app.py
edbad2b verified
Raw
History Blame Contribute Delete
1.49 kB
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()