Skip to content

victoruzoe/python-cli-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python CLI Calculator

Project Overview

This project implements a simple command-line calculator in Python that performs basic arithmetic operations on two numbers.
The calculator is designed to demonstrate the use of Python functions, dictionaries, loops, and user input handling.

The program allows users to perform calculations continuously by using the result of the previous operation as the starting number for the next calculation.


Checkpoint Objective

Create a calculator program that allows the user to perform mathematical operations on two numbers using basic functions and a dictionary to store the operations.

The program should also allow users to continue calculations using the result of previous calculations.


Program Instructions

The calculator program follows these steps:

  1. Create four basic mathematical functions:

    • add
    • subtract
    • multiply
    • divide

    Each function takes two numbers as input and returns the result of the operation.

  2. Create a dictionary called operations that maps operation symbols to their corresponding functions.

  3. Create a function called calculator that prompts the user to input the first number.

  4. Use a for loop to display the available operation symbols.

  5. Create a while loop that continues running until the user chooses to end the calculation.

  6. Inside the loop:

    • Prompt the user to select an operation symbol.
    • Prompt the user to input the second number.
  7. Use the dictionary to retrieve the function that corresponds to the selected operation symbol and store it in a variable called calculation_function.

  8. Perform the calculation by calling the selected function on the two numbers and store the result in a variable called answer.

  9. Print the equation and the result of the calculation.

  10. Ask the user if they would like to continue using the result as the first number for further calculations.

  11. If the user chooses to continue, update the first_number variable to the value of answer.

  12. If the user chooses to start a new calculation, request a new first number.


Demo

Calculator Demo


Features

  • Basic arithmetic operations
  • Continuous calculations using previous results
  • Command-line interaction
  • Error handling for invalid operations

Technologies Used

  • Python

How to Run the Program

Clone the repository

git clone https://github.com/victoruzoe/python-calculator-cli

Navigate to the project

cd python-calculator-cli

Run the program

python calculator.py

Example

Enter the first number: 10 Choose an operation: + Enter the second number: 5

Output: 10 + 5 = 15

About

Python command-line calculator demonstrating functions, dictionaries, and loop-based operations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages