Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import math | |
| # Initialize history in session state | |
| if "history" not in st.session_state: | |
| st.session_state.history = [] | |
| st.set_page_config(page_title="Advanced Calculator", page_icon="๐งฎ") | |
| st.title("๐งฎ Advanced Calculator") | |
| st.write("Select an operation from the menu below.") | |
| # Menu | |
| operation = st.selectbox( | |
| "Choose an operation", | |
| ( | |
| "Addition", | |
| "Subtraction", | |
| "Multiplication", | |
| "Division", | |
| "Power (x^y)", | |
| "Modulus", | |
| "Square Root", | |
| "Percentage", | |
| "Floor Division" | |
| ) | |
| ) | |
| # Two-number operations | |
| if operation != "Square Root": | |
| num1 = st.number_input("Enter first number", value=0.0) | |
| num2 = st.number_input("Enter second number", value=0.0) | |
| else: | |
| num = st.number_input("Enter number", value=0.0) | |
| if st.button("Calculate"): | |
| if operation == "Addition": | |
| result = num1 + num2 | |
| output = f"{num1} + {num2} = {result}" | |
| elif operation == "Subtraction": | |
| result = num1 - num2 | |
| output = f"{num1} - {num2} = {result}" | |
| elif operation == "Multiplication": | |
| result = num1 * num2 | |
| output = f"{num1} ร {num2} = {result}" | |
| elif operation == "Division": | |
| if num2 != 0: | |
| result = num1 / num2 | |
| output = f"{num1} รท {num2} = {result}" | |
| else: | |
| output = "โ Error! Division by zero." | |
| elif operation == "Power (x^y)": | |
| result = num1 ** num2 | |
| output = f"{num1} ^ {num2} = {result}" | |
| elif operation == "Modulus": | |
| result = num1 % num2 | |
| output = f"{num1} % {num2} = {result}" | |
| elif operation == "Percentage": | |
| if num2 != 0: | |
| result = (num1 / num2) * 100 | |
| output = f"{num1} is {result}% of {num2}" | |
| else: | |
| output = "โ Error! Cannot divide by zero." | |
| elif operation == "Floor Division": | |
| if num2 != 0: | |
| result = num1 // num2 | |
| output = f"{num1} // {num2} = {result}" | |
| else: | |
| output = "โ Error! Division by zero." | |
| elif operation == "Square Root": | |
| if num >= 0: | |
| result = math.sqrt(num) | |
| output = f"โ{num} = {result}" | |
| else: | |
| output = "โ Error! Cannot take square root of negative number." | |
| st.success(output) | |
| st.session_state.history.append(output) | |
| # History Section | |
| st.subheader("๐ Calculation History") | |
| if st.session_state.history: | |
| for item in st.session_state.history: | |
| st.write(item) | |
| else: | |
| st.write("No calculations yet.") | |
| # Clear History Button | |
| if st.button("Clear History"): | |
| st.session_state.history = [] | |
| st.success("History Cleared!") | |