Spaces:
Sleeping
Sleeping
| 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() | |