Skip to content

Scientific Calculator: A Small Command-Line Calculator in Python

A menu-driven calculator in Python. One file, nine operations, nothing to install.

ScientificCalculator is a small command-line program. You pick an operation from a numbered menu, type the numbers, and it prints the result. It keeps asking until you choose 0 to exit.

It is a learning project, and the README says so. The whole program is one file, calculator.py, about 90 lines, and it only uses Python's standard math module.

The name promises more than the program does. It covers basic arithmetic plus sine, cosine, tangent, logarithms and powers. That is the full scope.

What it does

  • Add, subtract, multiply and divide two numbers
  • Sine, cosine and tangent, with the angle in radians
  • Logarithm, with a base that defaults to e if you just press Enter
  • x to the power y, using math.pow

Two cases are checked explicitly. Dividing by zero, or taking the log of zero or a negative number, gives a clear message instead of a crash. Anything else that goes wrong inside an operation, like typing letters where a number belongs, is caught and printed as Error: followed by Python's own message.

How it works

Each operation is its own small function: add, subtract, multiply, divide, sine, cosine, tangent, logarithm and exponent. Most are a single line. divide and logarithm check their input first and raise a ValueError with a readable message.

main() prints the menu once and then loops. It reads your choice, converts each input with float(), calls the matching function and prints Result: with the value. A try/except around each operation turns any error into one line, so a typo does not end the session.

The loop only starts under if __name__ == "__main__", so another script can import the functions without getting the menu.

Where it started

The repository did not begin in Python. In November 2025 it held a C++ version that took whole expressions like 2 + 3 * 4, converted them to postfix and evaluated them, built with a Makefile. In February 2026 I deleted the C++ sources and the Makefile and replaced them with the Python file that is there now.

Running it

You need Python 3 and nothing else:

git clone https://git.mocipher.com/m01/ScientificCalculator.git
cd ScientificCalculator
python3 calculator.py

To reuse the math from your own code, import it from inside the project folder:

>>> from calculator import logarithm, exponent
>>> logarithm(8, 2)
3.0
>>> exponent(2, 10)
1024.0

What I'd do next

These are the real gaps, roughly in the order I would fix them:

  • Add tests. There are none. The functions have no side effects, so a short pytest file covering normal values and the two error cases would be quick to write.
  • Exit cleanly. The menu prompt sits outside the try block, so Ctrl+D at Enter choice:, or Ctrl+C anywhere, ends in a Python traceback instead of Goodbye!.
  • Check the log base. A base of 1 fails with a division-by-zero error, and a zero or negative base gets Python's generic math message. The base should be checked the same way x already is.
  • Friendlier input and output. You cannot type pi, so the sine of pi means typing 3.14159 by hand. Results are raw floats: 2 + 3 prints 5.0, and the log of 1000 to base 10 prints 2.9999999999999996. Accepting pi and e, offering degrees, and using math.log10 for base 10 would fix most of that.
  • One table instead of nine branches. main() repeats the same read, call, print steps in an if/elif chain. A dictionary from menu choice to function would shrink it and make it easy to reprint the menu, which today prints once and scrolls away.

It is a small program, and that is fine for what it was.