CoolFace
Apppublic

fm-engineer/PEC_Simple_Calculator

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py65 linesDownload Raw Back to root
1import streamlit as st2import math3 4st.title("🧮 Advanced Calculator")5 6# User inputs7num1 = st.number_input("Enter first number", value=0.0)8num2 = st.number_input("Enter second number", value=0.0)9 10# Operation choices11operations = [12    "Add", "Subtract", "Multiply", "Divide",13    "Exponentiation (x^y)", "Modulus (x % y)",14    "Square Root (√x)", "Log10 (log₁₀x)", "Natural Log (ln x)",15    "Sine (sin x)", "Cosine (cos x)", "Tangent (tan x)"16]17 18operation = st.selectbox("Select operation", operations)19 20# Perform calculation21result = None22 23try:24    if operation == "Add":25        result = num1 + num226    elif operation == "Subtract":27        result = num1 - num228    elif operation == "Multiply":29        result = num1 * num230    elif operation == "Divide":31        if num2 != 0:32            result = num1 / num233        else:34            st.error("Cannot divide by zero!")35    elif operation == "Exponentiation (x^y)":36        result = math.pow(num1, num2)37    elif operation == "Modulus (x % y)":38        result = num1 % num239    elif operation == "Square Root (√x)":40        if num1 >= 0:41            result = math.sqrt(num1)42        else:43            st.error("Cannot take square root of a negative number.")44    elif operation == "Log10 (log₁₀x)":45        if num1 > 0:46            result = math.log10(num1)47        else:48            st.error("Logarithm undefined for non-positive numbers.")49    elif operation == "Natural Log (ln x)":50        if num1 > 0:51            result = math.log(num1)52        else:53            st.error("Logarithm undefined for non-positive numbers.")54    elif operation == "Sine (sin x)":55        result = math.sin(math.radians(num1))56    elif operation == "Cosine (cos x)":57        result = math.cos(math.radians(num1))58    elif operation == "Tangent (tan x)":59        result = math.tan(math.radians(num1))60except Exception as e:61    st.error(f"An error occurred: {e}")62 63# Display result64if result is not None:65    st.success(f"Result: {result}")