Spaces:
Sleeping
Sleeping
File size: 3,223 Bytes
251d5e7 6278bc0 251d5e7 6278bc0 251d5e7 6278bc0 251d5e7 6278bc0 251d5e7 6278bc0 251d5e7 6278bc0 251d5e7 6278bc0 251d5e7 6278bc0 251d5e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import streamlit as st
import math
st.set_page_config(
page_title="Advanced Calculator",
layout="centered",
page_icon="๐งฎ"
)
st.title("๐งฎ Advanced Calculator")
# Initialize history if not already present
if 'history' not in st.session_state:
st.session_state.history = []
# Layout inputs side by side
col1, col2 = st.columns(2)
with col1:
num1 = st.number_input("First Number", format="%.4f", key="num1")
with col2:
num2 = st.number_input("Second Number", format="%.4f", key="num2")
# Operation selection
operation = st.selectbox("Select Operation", [
"โ Addition",
"โ Subtraction",
"โ๏ธ Multiplication",
"โ Division",
"๐ฐ Power (num1^num2)",
"โ Square Root of First Number",
"% Percentage (num1% of num2)",
"1/x Reciprocal of First Number"
])
# Result calculation
result = None
symbol = ""
error = None
if st.button("๐ข Calculate"):
try:
if operation == "โ Addition":
result = num1 + num2
symbol = "+"
elif operation == "โ Subtraction":
result = num1 - num2
symbol = "-"
elif operation == "โ๏ธ Multiplication":
result = num1 * num2
symbol = "*"
elif operation == "โ Division":
if num2 != 0:
result = num1 / num2
symbol = "/"
else:
error = "Division by zero is not allowed."
elif operation == "๐ฐ Power (num1^num2)":
result = math.pow(num1, num2)
symbol = "^"
elif operation == "โ Square Root of First Number":
if num1 >= 0:
result = math.sqrt(num1)
symbol = "โ"
else:
error = "Cannot take square root of a negative number."
elif operation == "% Percentage (num1% of num2)":
result = (num1 / 100) * num2
symbol = "%"
elif operation == "1/x Reciprocal of First Number":
if num1 != 0:
result = 1 / num1
symbol = "1/x"
else:
error = "Reciprocal of 0 is undefined."
# Display result
if error:
st.warning(f"โ ๏ธ {error}")
else:
if operation == "โ Square Root of First Number":
st.success(f"Result: โ{num1} = {result}")
st.session_state.history.append(f"โ{num1} = {result}")
elif operation == "1/x Reciprocal of First Number":
st.success(f"Result: 1/{num1} = {result}")
st.session_state.history.append(f"1/{num1} = {result}")
else:
st.success(f"Result: {num1} {symbol} {num2} = {result}")
st.session_state.history.append(f"{num1} {symbol} {num2} = {result}")
except Exception as e:
st.error(f"An error occurred: {str(e)}")
# Show history
if st.session_state.history:
st.write("### ๐ Calculation History")
for record in reversed(st.session_state.history[-5:]): # show last 5 only
st.write(record)
# Reset button
if st.button("๐ Reset Calculator"):
st.session_state.history.clear()
st.experimental_rerun()
|