Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import math | |
| st.set_page_config(page_title="Simple Calculator", layout="centered") | |
| st.title("🧮 Simple Calculator") | |
| # Operation selection | |
| operation = st.selectbox( | |
| "Choose Operation", | |
| [ | |
| "Addition", | |
| "Subtraction", | |
| "Multiplication", | |
| "Division", | |
| "Square Root", | |
| "Power", | |
| "Log (ln)" | |
| ] | |
| ) | |
| # Inputs | |
| if operation in ["Addition", "Subtraction", "Multiplication", "Division", "Power"]: | |
| num1 = st.number_input("Enter first number", value=0.0) | |
| num2 = st.number_input("Enter second number", value=0.0) | |
| elif operation in ["Square Root", "Log (ln)"]: | |
| num1 = st.number_input("Enter number", value=0.0) | |
| # Calculate button | |
| if st.button("Calculate"): | |
| result = None | |
| try: | |
| if operation == "Addition": | |
| result = num1 + num2 | |
| elif operation == "Subtraction": | |
| result = num1 - num2 | |
| elif operation == "Multiplication": | |
| result = num1 * num2 | |
| elif operation == "Division": | |
| if num2 == 0: | |
| result = "Error: Division by zero!" | |
| else: | |
| result = num1 / num2 | |
| elif operation == "Square Root": | |
| if num1 < 0: | |
| result = "Error: Negative number!" | |
| else: | |
| result = math.sqrt(num1) | |
| elif operation == "Power": | |
| result = math.pow(num1, num2) | |
| elif operation == "Log (ln)": | |
| if num1 <= 0: | |
| result = "Error: Log undefined!" | |
| else: | |
| result = math.log(num1) | |
| except Exception as e: | |
| result = f"Error: {e}" | |
| st.subheader("Result:") | |
| st.write(result) |