Muhammadtaha12's picture
Update app.py
03fa135 verified
import streamlit as st
import math
# Define the calculator functions
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Error! Division by zero."
else:
return x / y
def square_root(x):
if x < 0:
return "Error! Square root of negative number."
else:
return math.sqrt(x)
def power(x, y):
return math.pow(x, y)
def sine(x):
return math.sin(math.radians(x))
def cosine(x):
return math.cos(math.radians(x))
def tangent(x):
return math.tan(math.radians(x))
def logarithm(x, base=10):
if x <= 0:
return "Error! Logarithm of non-positive number."
else:
return math.log(x, base)
# Streamlit UI for the calculator
st.title("Scientific Calculator")
# Input fields for the user
num1 = st.number_input("Enter first number:", value=0.0)
num2 = st.number_input("Enter second number (optional):", value=0.0)
# Dropdown for operation selection
operation = st.selectbox("Choose an operation:", [
"Add", "Subtract", "Multiply", "Divide",
"Square Root", "Power", "Sine", "Cosine", "Tangent", "Logarithm"
])
# Handle different operations
if operation == "Add":
result = add(num1, num2)
elif operation == "Subtract":
result = subtract(num1, num2)
elif operation == "Multiply":
result = multiply(num1, num2)
elif operation == "Divide":
result = divide(num1, num2)
elif operation == "Square Root":
result = square_root(num1)
elif operation == "Power":
result = power(num1, num2)
elif operation == "Sine":
result = sine(num1)
elif operation == "Cosine":
result = cosine(num1)
elif operation == "Tangent":
result = tangent(num1)
elif operation == "Logarithm":
result = logarithm(num1)
# Display the result
st.write(f"Result: {result}")