0

I have to write some arithmetic script. The script should be launched like this:

$ python name_script.py 2 + 2
$ python name_script.py 2 * 2

To enter data in one line, I use argparse. But I have a problem with multiplication (*) and division (/), argparse doesn't perform them as mathematical operations. How to make argparse perceive input (*) as multiplication?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 2
    This has nothing to do with Python or argparse: you *must* escape `*` on the command line, otherwise your shell will expand it as a wildcard. Python can’t do anything to prevent this, since it happens *before* Python is launched. In other words: there is no way to meet the stated requirements. – Konrad Rudolph Aug 26 '21 at 09:24
  • @RonnyOutside : You can detect this kind of bug easily if you do a `set -x` in your shell before running the Python command. – user1934428 Aug 26 '21 at 09:58
  • My script expects three input parameters: digit, arithmetic operation (+, -, * or /), digit. And in the form, as indicated in the example. Is it really unrealizable? – Ronny Outside Aug 26 '21 at 10:08
  • argparse doesn't do any math; the rest of your script might. But you haven't shown that code, nor any results or errors. And check `sys.argv` if the input of '*' seems funny. – hpaulj Aug 26 '21 at 14:35

0 Answers0