Objective
Create a new version of the program *Calculator with command-line parameters* that finishes with the following exit codes:
- Exit with code
1if the total number of parameters is different from 3. - Exit with code
2if the second parameter is not a valid arithmetic operator (+,-,x,*,/). - Exit with code
3if the first or third parameter is not a valid number. - Exit with code
0if everything is correct.
Instructions
Modify the original program so that it ends with sys.exit(code) according to the conditions above. Check each case in the terminal (in Linux and macOS you can see the last exit code with echo $?).
Arguments and exit code: the program receives three arguments in sys.argv: an integer, the operator (+, -, x, * or /) and another integer. If everything is correct, print the result (the division gives a decimal number) and finish with code 0. In case of error, print nothing and finish with the code of the first error found, checking in this order: 1 if the number of arguments is not 3; 2 if the operator is not valid; 3 if the first or the third argument is not an integer. Assume the divisor is never 0.
8
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise