evaluating a string

Joal Heagney s713221 at student.gu.edu.au
Wed Oct 18 04:23:51 EDT 2000


Okay, sorry about that. How embarassing. Perhaps I should code before opening
my mouth, huh? Anycase, here is a baby implementation of a string evaluator
that doesn't get hung up on integer division.

>>> def string_eval(text):
...     splittext = re.split("/", text)
...     text2 = ""
...     for i in range(len(splittext)-1):
...             text2 = text2 + splittext[i] + ".0/"
...     text2 = text2 + splittext[-1]
...     return eval(text2)
...
>>> string_eval("1+2*3/4")
2.5
>>>

Joal Heagney/AncientHart




More information about the Python-list mailing list