A console-based calculator built with Python, combining functions, Object-Oriented Programming (OOP), and a modular file structure. The project demonstrates clean code organization across multiple modules, reusable static methods, exception handling, JSON-based data persistence, and an OOP-based history management system.
- Add
- Subtract
- Multiply
- Divide
- Power
- Modulus
- Square Root
- Menu-Driven Interface
- Input Validation
- Division/Modulus by Zero Handling
- Negative Number Check for Square Root
- Persistent Calculation History (JSON)
- View History
- Clear History
- Exception Handling (
try/except/raise) - OOP-based History Management
- Modular Codebase (logic, history, and entry point separated into their own files)
- Python 3
- JSON
- Math Module
Calculator/
│
├── main.py
├── calculator.py
├── history_manager.py
├── __init__.py
├── .gitignore
└── README.md
calculator.py— theCalculatorclass, holding all math operations as static methodshistory_manager.py— theHistoryManagerclass, handling load/save/view/clear of calculation historymain.py— the entry point: menu display, user input, and program flow, importing from the two modules above
Note:
calculator_history.jsonis created automatically when the program runs. It stores calculation history locally and is excluded from the repository using.gitignorebecause it contains runtime data.
- Clone the repository
git clone https://github.com/osaid400/Calculator-Using-Functions.git- Navigate to the project directory
cd Calculator-Using-Functions- Run the program
python main.py- Functions
- Parameters & Return Values
- Conditional Statements
- Loops
- Menu-Driven Programs
- Dictionaries
- Exception Handling
- Raising Custom Exceptions
- Input Validation
- File Handling with JSON
- String Formatting
- Classes
- Constructors (
__init__) - Static Methods
- Object Creation
- Encapsulation (
HistoryManagermanaging its own data) - Separation of Responsibilities
- Splitting a single-file project into logical modules (
calculator.py,history_manager.py,main.py) importandfrom...importacross files- Cross-module references (e.g.,
main.pyusing bothCalculatorandHistoryManager) if __name__ == "__main__":to control what runs when a file is executed directly vs. imported
========= Calculator =========
1. Add
2. Subtract
3. Multiply
4. Divide
5. Power
6. Modulus
7. Square Root
8. View History
9. Clear History
0. Exit
==============================
Enter choice: 4
Enter first number: 10
Enter second number: 0
Division by zero is not allowed.
Enter choice: 1
Enter first number: 15
Enter second number: 5
Result: 20.0
================ Calculator History ================
1. 15.0 + 5.0 = 20
2. 10.0 ÷ 2.0 = 5
3. √16.0 = 4
====================================================
- Every calculation is stored in
calculator_history.json. - History is managed through the
HistoryManagerclass (inhistory_manager.py). - Previous calculations remain available even after closing the program.
- Users can view or clear history from the main menu.
- Build a GUI version using Tkinter
- Add scientific calculator functions (sin, cos, tan, log, factorial)
- Export history to TXT or CSV
- Add keyboard shortcut support
Muhammad Abdullah Farooq
GitHub: https://github.com/osaid400