Skip to main content

Calculator Project

Project description

Calculator

This is a simple calculator program written in Python that performs basic arithmetic operations. The calculator uses the Decimal module to ensure precision in calculations.

The following math operations are supported:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Root (**)

Installation

Use the following command to install

pip install calculator_project_231023

Usage

To use the calculator, you can run the run_interface() function. The function will prompt you for inputs and perform the specified calculations. You can continue with the result, reset the calculator, or quit the program.

To use the calculator, follow these steps:

  1. Import the pacakge.
  2. Run the main interface function run_interface().
  3. You will be prompted to enter the first number (integer or float).
  4. After entering the first number, you will be prompted to choose an arithmetic operation (+, -, *, /, **) for the calculation.
  5. Enter the second number (integer or float).
  6. The program will perform the selected arithmetic operation on the first and second numbers and display the result.
  7. You will then be prompted to choose the next action:
  • Enter 'C' to continue using the current result as the first number for the next operation.
  • Enter 'R' to reset the calculator result to 0 and start a new calculation.
  • Enter 'Q' to quit the program.
  1. Repeat the calculation loop as desired.

Example with the user interface

import calculator_project_231023

run_interface()

Your interaction will be something like this:

Welcome to the calculator
Use it to add, subtract, multiply, divide, or take a root

Enter the first element (integer or float): 5
Enter arithmetic operation (options: +, -, *, /, **): *
Enter the second element (integer or float): 2
Result: 10

C to continue with the result, R to reset result to 0, Q to quit: c
Enter arithmetic operation (options: +, -, *, /, **): +
Enter the second element (integer or float): 3
Result: 13

C to continue with the result, R to reset result to 0, Q to quit: c
Enter arithmetic operation (options: +, -, *, /, **): **
Enter the second element (integer or float): 0.5
Result: 3.605551275

C to continue with the result, R to reset result to 0, Q to quit: R
Calculator reset to 0

Enter the first element (integer or float): 10
Enter arithmetic operation (options: +, -, *, /, **): /
Enter the second element (integer or float): 3
Result: 3.3333333333

C to continue with the result, R to reset result to 0, Q to quit: Q
Thank you for using the calculator

Example with directly using Class methods

from calculator_project_231023 import Calculator
from decimal import Decimal

calculator = Calculator()  # Create an instance of the calculator

calculator.perform_operation('+', Decimal(5))  # Add 5 to the current result
calculator.perform_operation('*', Decimal(2))  # Multiply the current result by 2
calculator.perform_operation('/', Decimal(3))  # Divide the current result by 3

calculator.add(4)   # Add 4 to the current result
calculator.root(2)  # Take the square root of the current result
print(my_calculator.result) # Print the current calculator result
calculator.clear_result()  # Reset the calculator result to 0

Notes

  • The calculator uses the Decimal data type for more precise calculations. The precision of the calculator can be adjusted by modifying the PRECISION constant in the code. It is 10 digits by default.
  • The calculator stores the most recent result and applies additional operations to that result until explicitly instructed to clear the result.
  • The calculator supports the following arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/), and taking the root (**).
  • To take the root of a specific power you only need to inter the power. For instace to take a 3rd degree root of 8 you enter 3 after selecting the ** operation. Your input sequence looks like this 8 ** 3, but the actual calculation happens in the following format: 8**(1/3).
  • The calcualtor does not allow to take a root of a negative number. But it allows to enter a negavite power, for example your input sequence could look like this 8 ** -3, but the actual calculation happens in the following format: 8**(1/-3).
  • The calculator prompts the user for valid number inputs and handles any invalid inputs with appropriate error messages. The calcualtor exits after 3 consequitive invalid inputs.
  • The calculator allows the user to continue using the current result for subsequent operations, reset the calculator result to 0, or quit the program.
  • The program can be terminated at the end of one calculation cycle by entering 'Q' as the next action.

Documentation

Calculator class

clear_result() Resets the calculator result to 0.

add(addend: Decimal) Adds the given addend to the current result.

subtract(subtrahend: Decimal) Subtracts the given subtrahend from the current result.

multiply(multiplier: Decimal) Multiplies the current result by the given multiplier.

divide(divisor: Decimal) Divides the current result by the given divisor.

root(degree: Decimal) Calculates the root of the current result with the given degree.

perform_operation(operator: str, operand: Decimal) Performs the specified operation on the current result with the given operand.

Functions

run_interface() The main function for interfacing between the user and the calculator class. It sets the decimal calculation precision, initiates the calculator, requestse user inputs, initiates calculator operations and controls when to quit or reset the calculator.

get_valid_number() Prompts the user for a valid number input and returns the valid input as a Decimal or information to exit the calculator.

get_valid_operation() Prompts the user for a valid operation input and returns the valid input as a string or information to exit the calculator.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

calculator_project_231023-1.0.4.tar.gz (9.5 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page