Skip to main content

Calculator with memory, supports + - / * sqrt()

Project description

Calculator — Simple Memory-Based Arithmetic Module

A tiny Python class that keeps a running memory and performs validated arithmetic operations.

This document explains the module, how to install/use it. It also includes a mini‑tutorial in English


Features

  • Calculator.memory — read‑only property exposing the current memory value.
  • Validated inputs: only int or float are accepted; other types raise TypeError.
  • Arithmetic: add, subtract, multiply, divide.
  • Utilities: sqrt (adds в€љn to memory), memory_reset (resets to 0.0).
  • Clear, Google‑style docstrings and helpful error messages.

Installation

This is a single‑file module. You can:

  • drop calculator.py into your project, or
  • install it as an editable local package if you later add pyproject.toml/setup.cfg.

Python version: 3.9+ recommended.


Quick Start (Tutorial)

1) Import and construct

from calculator import Calculator

c = Calculator()
print(c.memory)   # 0.0

2) Basic operations

c.add(10)         # -> 10.0
c.subtract(3.5)   # -> 6.5
c.multiply(2)     # -> 13.0
c.divide(4)       # -> 3.25

3) Square root accumulation

c.sqrt(9)         # adds 3, -> 6.25

4) Reset

c.memory_reset()  # -> 0.0

5) Defensive behavior (errors)

try:
    c.add("3")  # not allowed
except TypeError as e:
    print(e)    # Argument must be an int or float

try:
    c.divide(0) # division by zero
except ZeroDivisionError as e:
    print(e)

try:
    c.sqrt(-1)  # negative sqrt
except ValueError as e:
    print(e)    # Cannot take square root of a negative real number

# read-only property
try:
    c.memory = 42
except AttributeError as e:
    print(e)    # memory is read-only

API Reference

Class: Calculator()

  • Properties
    • memory: float — read‑only; returns the current memory value.
  • Methods
    • add(n: float) -> float — Adds n to memory.
    • subtract(n: float) -> float — Subtracts n from memory.
    • multiply(n: float) -> float — Multiplies memory by n.
    • divide(n: float) -> float — Divides memory by n. Raises ZeroDivisionError if n == 0.
    • sqrt(n: float) -> float — Adds sqrt(n) to memory. Raises ValueError if n < 0.
    • memory_reset() -> float — Resets memory to 0.0.

Validation rule: a private helper _to_float converts and validates inputs. Only int and float are accepted


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

simple-calculator-tc2-1.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_calculator_tc2-1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file simple-calculator-tc2-1.0.tar.gz.

File metadata

  • Download URL: simple-calculator-tc2-1.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for simple-calculator-tc2-1.0.tar.gz
Algorithm Hash digest
SHA256 dcee17969fea5271ba6cffa5940edd2f41656eda8a25cfd892787ba300e559c6
MD5 8073d93db4661b94006663959c9b376c
BLAKE2b-256 8870ce7f1fb25bdde2b7090a8812431078dec661cb06a40b5f4d57dc49660fe9

See more details on using hashes here.

File details

Details for the file simple_calculator_tc2-1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_calculator_tc2-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f87100312483f33916a3d8394e9ba64c197d0eff5b3ea5e1190f3010a5df1b68
MD5 cb45d6da33a6fb1dad606b56f6754453
BLAKE2b-256 6c232c1fd5bdb7d60907a0b94500d633b9eeb961db9f2d4ca90634824a4a9709

See more details on using hashes here.

Supported by

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