Spaces:
Build error
Build error
| import streamlit as st | |
| # Function to perform arithmetic operations | |
| def calculate(num1, num2, operation): | |
| if operation == 'Add': | |
| return num1 + num2 | |
| elif operation == 'Subtract': | |
| return num1 - num2 | |
| elif operation == 'Multiply': | |
| return num1 * num2 | |
| elif operation == 'Divide': | |
| if num2 == 0: | |
| return "Cannot divide by zero!" | |
| return num1 / num2 | |
| else: | |
| return "Invalid operation" | |
| # Streamlit UI - Customizing the interface for better user experience | |
| # Title with custom styling | |
| st.markdown(""" | |
| <h1 style="text-align: center; color: #4CAF50;">Simple Calculator</h1> | |
| <p style="text-align: center; font-size: 18px;">Perform basic arithmetic operations: Add, Subtract, Multiply, Divide.</p> | |
| """, unsafe_allow_html=True) | |
| # Create a layout with columns for inputs and result | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| # User input for the first number | |
| num1 = st.number_input("Enter the first number", value=0.0, format="%.2f", step=0.1, key="num1") | |
| with col2: | |
| # User input for the second number | |
| num2 = st.number_input("Enter the second number", value=0.0, format="%.2f", step=0.1, key="num2") | |
| # Selectbox for the operation | |
| operation = st.selectbox( | |
| "Select the operation", | |
| ("Add", "Subtract", "Multiply", "Divide"), | |
| key="operation" | |
| ) | |
| # Add a button to calculate | |
| if st.button("Calculate"): | |
| result = calculate(num1, num2, operation) | |
| st.markdown(f"### **Result: {result}**", unsafe_allow_html=True) | |
| # Additional styling | |
| st.markdown(""" | |
| <style> | |
| .css-1v0mbdj {padding: 0.5rem 2rem;} | |
| .css-1cpxqw2 {background-color: #4CAF50; color: white;} | |
| .stButton button {border-radius: 5px;} | |
| </style> | |
| """, unsafe_allow_html=True) | |