Spaces:
Sleeping
Sleeping
import streamimport streamlit as st | |
import math | |
st.title("Advanced Calculator") | |
# Input fields | |
num1 = st.number_input("Enter first number", format="%.2f") | |
operation = st.selectbox( | |
"Select operation", | |
["+", "-", "*", "/", "^ (power)", "% (modulus)", "// (floor division)", "β (square root)", "log10"] | |
) | |
num2 = st.number_input("Enter second number", format="%.2f") | |
result = None | |
error = 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 = "Cannot divide by zero" | |
elif operation == "^ (power)": | |
result = num1 ** num2 | |
elif operation == "% (modulus)": | |
if num2 != 0: | |
result = num1 % num2 | |
else: | |
error = "Cannot perform modulus by zero" | |
elif operation == "// (floor division)": | |
if num2 != 0: | |
result = num1 // num2 | |
else: | |
error = "Cannot perform floor division by zero" | |
elif operation == "β (square root)": | |
if num1 >= 0: | |
result = math.sqrt(num1) | |
else: | |
error = "Cannot take square root of a negative number" | |
elif operation == "log10": | |
if num1 > 0: | |
result = math.log10(num1) | |
else: | |
error = "Logarithm defined only for positive numbers" | |
except Exception as e: | |
error = str(e) | |
if error: | |
st.error(error) | |
else: | |
st.success(f"Result: {result:.4f}") | |
lit as st | |
st.title("Simple Calculator") | |
# Input fields | |
num1 = st.number_input("Enter first number", format="%.2f") | |
operation = st.selectbox("Select operation", ["+", "-", "*", "/"]) | |
num2 = st.number_input("Enter second number", format="%.2f") | |
# Perform calculation | |
result = None | |
error = 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 = "Cannot divide by zero" | |
except Exception as e: | |
error = str(e) | |
if error: | |
st.error(error) | |
else: | |
st.success(f"Result: {result:.2f}") | |