parsing math expression in python and solving to find an answer

后端 未结 3 550
Happy的楠姐
Happy的楠姐 2020-12-10 10:07

I am quite new to programming. This is in relation to python. So the idea is to take an expression such as 3/5 or, at most, 3/5*2(at most two operators, note that the operat

3条回答
  •  -上瘾入骨i
    2020-12-10 10:35

    Use the shlex and StringIO Python module. In Python 2.3+:

    >>> from StringIO import StringIO
    >>> import shlex
    >>> input = StringIO('3/4+5')
    >>> list(shlex.shlex(input))
    ['3', '/', '4', '+', '5']
    

提交回复
热议问题