Spaces:
Sleeping
Sleeping
File size: 2,207 Bytes
284d101 | 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 | import streamlit as st
def main():
st.title("Enhanced Calculator")
# Select an operation
operations = [
"Addition",
"Subtraction",
"Multiplication",
"Division",
"Modulus",
"Exponentiation",
"Average"
]
operator = st.selectbox("Select an operation", operations)
# For exponentiation, ask for base and exponent.
if operator == "Exponentiation":
base = st.number_input("Enter the base value", value=0.0)
exponent = st.number_input("Enter the exponent value", value=0.0)
else:
# For all other operations, ask for three numbers.
num1 = st.number_input("Enter the first number", value=0.0)
num2 = st.number_input("Enter the second number", value=0.0)
num3 = st.number_input("Enter the third number", value=0.0)
if st.button("Calculate"):
result = None
try:
if operator == "Addition":
result = num1 + num2 + num3
elif operator == "Subtraction":
result = num1 - num2 - num3
elif operator == "Multiplication":
result = num1 * num2 * num3
elif operator == "Division":
# Check for division by zero
if num2 == 0 or num3 == 0:
st.error("Error: Division by zero is not allowed.")
else:
result = num1 / num2 / num3
elif operator == "Modulus":
# Check for modulus by zero
if num2 == 0 or num3 == 0:
st.error("Error: Modulus by zero is not allowed.")
else:
result = (num1 % num2) % num3
elif operator == "Exponentiation":
# Now we take an input value for the exponent and compute base**exponent.
result = base ** exponent
elif operator == "Average":
result = (num1 + num2 + num3) / 3
if result is not None:
st.success(f"Result: {result}")
except Exception as e:
st.error(f"An error occurred: {e}")
if __name__ == '__main__':
main()
|