Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: Calculator eval

import sys

def main():
    if len(sys.argv) != 4:
        exit(f"Usage: {sys.argv[0]} NUMBER OPERATOR NUMBER")

    command = sys.argv[1] + sys.argv[2] + sys.argv[3]
    print(command)
    res = eval(command)
    print(res)

main()
$ python examples/basics/calculator_argv_eval.py 2 + 3
5

$ python examples/basics/calculator_argv_eval.py 2 '*' 3
6
  • Now forget this and don't use eval for the next few years!