Skip to content

About

Really simple, limited calculator. Takes two numbers and lets you add, subtract, multiply or divide them.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

17 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

basic-calculator

Really simple, limited calculator. Two numbers are read, one of four operations is chosen, and the result is printed.

Requirements

Python 2. The program uses print statements, so it is rejected at parse time by Python 3 and does not run there at all. It also calls raw_input, which Python 3 does not define. It has been verified under Python 2.7.18.

Which interpreter this project should target going forward is an open question — see issue #1.

Run

python calculator.py

Four things are read from standard input, in this order: the first number, the second number, the operation, and a final line that dismisses the exit prompt. A number that can't be parsed skips straight to that final line, so a run that stops there reads only two or three lines.

Every line the program asks for is required, including the last. An unattended run that supplies too few waits at whichever prompt it reached for as long as standard input stays open, and raises EOFError once it closes — so omitting only the final line still prints the answer first and fails afterwards. Wrapping such a run in timeout is advisable.

Operations

The operation is matched case-insensitively against exactly these four words, spelled as the prompt spells them:

Type Result
Add the two numbers added
Subtract the second number subtracted from the first
Multiply the two numbers multiplied
Divide the first number divided by the second, followed by the remainder

The legacy spelling Subract is also accepted for backwards compatibility with previous versions of the prompt.

Surrounding whitespace is stripped before matching, so a stray leading or trailing space is ignored. Anything else prints That wasn't an option. The options are Add, Subtract, Multiply or Divide. and then waits at the exit prompt.

A second number of 0 combined with Divide prints The second number can't be zero. instead of dividing. Every other combination of the four operations and two numbers is computed.

Anything Python's float can't parse at either number prompt — including an empty line — prints That wasn't a number. straight away, skipping the remaining prompts, and then waits at the exit prompt. There is no chance to retry; the program has to be run again.

float also parses scientific notation and the words inf and nan, so those count as numbers: 1e3 is read as 1000.0, and nan plus 3 prints The answer is nan.

Example

Transcript of a real run under Python 2.7.18. Input was piped rather than typed, which is why the three prompts appear on one line with no echoed answers:

$ printf '6\n3\nadd\n\n' | python calculator.py
What is the first number? What is the second number? Add, Subtract, Multiply or Divide? The answer is  9.0

Press Enter to exit.

The Divide branch additionally reports the remainder:

$ printf '7\n2\ndivide\n\n' | python calculator.py
What is the first number? What is the second number? Add, Subtract, Multiply or Divide? The answer is  3.5  with  1.0  left over.

Press Enter to exit.

Check

sh check.sh

Pipes canned input into the program once per operation, plus the rejected-operation, zero-divisor and not-a-number paths, and checks the printed answer and exit status of each. It prints SKIP: no Python 2 interpreter and exits 0 when neither python2 nor a Python 2 python is found.

Known limitations

The following was reproduced under Python 2.7.18 and is tracked as an issue:

  • Both operands are floats, so Divide reports a true-division quotient and a float remainder — the same leftover twice (#8).

License

Stephenson Software Non-Commercial License. See LICENSE.

About

Really simple, limited calculator. Takes two numbers and lets you add, subtract, multiply or divide them.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages