markdown Copy code
The Basic Calculator is a simple command-line application that performs basic arithmetic operations:
- Addition
- Subtraction
- Multiplication
- Division
The calculator takes user input for two numbers and an operator, then displays the result.
- Functions and Code Organization
- User Input (
input()
function) - Conditional Statements (
if
,elif
,else
) - Exception Handling (
try
,except
) - Arithmetic Operations
- Basic Data Types (
int
,float
,str
)
- Python 3.x installed on your machine.
- Access to a command-line interface (CLI) or terminal.
-
Navigate to the project directory:
cd basic_calculator
Run the calculator: bash Copy code python main.py Follow the on-screen instructions to perform calculations. π Usage Instructions
Upon running the calculator, you will be prompted to:
Enter the first number. Enter an operator (+, -, *, /). Enter the second number. The calculator will then display the result of the operation.
Example: mathematica Copy code Enter the first number: 10 Enter an operator (+, -, *, /): * Enter the second number: 5 Result: 10.0 * 5.0 = 50.0 π οΈ Code Overview
The application is structured with a main function and separate functions for each arithmetic operation:
add(a, b): Returns the sum of a and b. subtract(a, b): Returns the difference of a and b. multiply(a, b): Returns the product of a and b. divide(a, b): Returns the quotient of a divided by b. calculate(a, operator, b): Calls the appropriate function based on the operator. Modules Used sys: For system-specific parameters and functions (not used in this version but useful for future enhancements). π€ Contributing
Contributions are welcome! If you have ideas to enhance this calculator, feel free to fork the repository and submit a pull request.
π§ Contact
If you have any questions or suggestions, feel free to reach out by creating an issue in the repository.
Code Explanation and Best Practices
- Modular Functions
Arithmetic Functions: Separate functions for each operation (add, subtract, multiply, divide) make the code reusable and easier to maintain. calculate(a, operator, b): Centralizes the operation selection logic, making it easy to extend with more operators in the future. 2. User Input Handling
Input Validation: Numbers: Uses float() conversion to handle both integers and decimals. Operator: Checks if the operator is one of the accepted symbols. Graceful Exit: Allows the user to quit by entering 'q' when prompted for the first number. Exception Handling: ValueError: Catches invalid numerical inputs. ZeroDivisionError: Handles division by zero in the divide function. 3. User Experience
Clear Instructions: Prompts guide the user on what is expected at each step. Result Display: Shows the full expression and the result for clarity. Loop Control: Allows the user to perform multiple calculations without restarting the program. 4. Code Readability
Comments and Docstrings: Functions include docstrings that explain their purpose. Consistent Naming: Variables and functions have descriptive names. Whitespace and Indentation: Follows Python's standard for readability. 5. Extensibility
Easy to Add Features: The structure allows for additional operations (e.g., exponentiation) to be added with minimal changes. Modular Design: Separating logic into functions makes future modifications straightforward. Enhancements and Extensions Consider adding the following features to enhance the calculator:
Additional Operations: Implement modulus (%), exponentiation (**), or floor division (//). Parentheses Handling: Allow users to input expressions with parentheses and parse them accordingly. Input History: Keep a history of calculations performed during the session. Error Messages: Provide more detailed error messages for invalid inputs. GUI Version: Create a graphical user interface using Tkinter or another library. Testing the Calculator Valid Inputs: Test calculations with valid numbers and operators. Invalid Inputs: Enter invalid numbers (e.g., letters) and operators to ensure errors are handled gracefully. Division by Zero: Attempt to divide by zero to see if the appropriate error message is displayed. Exiting the Program: Test the exit functionality by entering 'q' when prompted.