Skip to content

About

A console-based Python calculator with OOP design, JSON-based history persistence, and a modular file structure.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

13 Commits

Folders and files

Repository files navigation

Calculator (Python)

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.

Features

  • 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)

Technologies Used

  • Python 3
  • JSON
  • Math Module

Project Structure

Calculator/
│
├── main.py
├── calculator.py
├── history_manager.py
├── __init__.py
├── .gitignore
└── README.md
  • calculator.py — the Calculator class, holding all math operations as static methods
  • history_manager.py — the HistoryManager class, handling load/save/view/clear of calculation history
  • main.py — the entry point: menu display, user input, and program flow, importing from the two modules above

Note: calculator_history.json is created automatically when the program runs. It stores calculation history locally and is excluded from the repository using .gitignore because it contains runtime data.

How to Run

  1. Clone the repository
git clone https://github.com/osaid400/Calculator-Using-Functions.git
  1. Navigate to the project directory
cd Calculator-Using-Functions
  1. Run the program
python main.py

Concepts Practiced

Python Fundamentals

  • Functions
  • Parameters & Return Values
  • Conditional Statements
  • Loops
  • Menu-Driven Programs
  • Dictionaries
  • Exception Handling
  • Raising Custom Exceptions
  • Input Validation
  • File Handling with JSON
  • String Formatting

Object-Oriented Programming (OOP)

  • Classes
  • Constructors (__init__)
  • Static Methods
  • Object Creation
  • Encapsulation (HistoryManager managing its own data)
  • Separation of Responsibilities

Modules & Packages

  • Splitting a single-file project into logical modules (calculator.py, history_manager.py, main.py)
  • import and from...import across files
  • Cross-module references (e.g., main.py using both Calculator and HistoryManager)
  • if __name__ == "__main__": to control what runs when a file is executed directly vs. imported

Example Output

Main Menu

========= Calculator =========
1. Add
2. Subtract
3. Multiply
4. Divide
5. Power
6. Modulus
7. Square Root
8. View History
9. Clear History
0. Exit
==============================

Division by Zero

Enter choice: 4

Enter first number: 10
Enter second number: 0
Division by zero is not allowed.

Successful Calculation

Enter choice: 1

Enter first number: 15
Enter second number: 5

Result: 20.0

History

================ Calculator History ================
 1. 15.0 + 5.0            = 20
 2. 10.0 ÷ 2.0            = 5
 3. √16.0                 = 4
====================================================

How History Works

  • Every calculation is stored in calculator_history.json.
  • History is managed through the HistoryManager class (in history_manager.py).
  • Previous calculations remain available even after closing the program.
  • Users can view or clear history from the main menu.

Future Improvements

  • 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

Author

Muhammad Abdullah Farooq

GitHub: https://github.com/osaid400

About

A console-based Python calculator with OOP design, JSON-based history persistence, and a modular file structure.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages