Command-line programs · 8 / 15 Exercise Medium

Calculator with argparse

Objective

Create a command-line calculator with the argparse module, which validates the arguments for you. The arguments of each test are passed to the program as if you typed them in the terminal (they appear in the test cases).

Instructions

  • Define three positional arguments: a (integer), op (it can only be +, -, x or /, with choices) and b (integer).
  • Define the option --round (integer, default 2): decimals used to round a division.
  • Show Result: r. Division is a / b rounded with round(value, decimals).
  • If dividing by 0 call parser.error(...) with a message; argparse ends the program with exit code 2 (just like when an argument is invalid).
Output
Result: 42

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise