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 os

def main():
    a = input("Number: ")
    b = input("Number: ")
    op = input("Operator (+-*/): ")

    command = a + op + b
    print(command)
    res = eval(command)
    print(res)

main()
$ python examples/basics/calculator_eval.py

Number: 2
Number: 3
Operator (+-*/): +
2+3
5

Try Again, this time:

$ python examples/basics/calculator_eval.py

Number: os.system("ls -l")
Number:
Operator (+-*/):

And then you could try it with rm -rf / or if you are on Windows try os.system("dir") or this: os.system("rm -f calculator_eval.py") and on windows it would be os.system("del calculator_eval.py").

  • Now forget this and don't use eval for the next few years!