Discussion:
evaluating a string
piet
2000-10-18 15:43:51 UTC
Permalink
JH> Okay, sorry about that. How embarassing. Perhaps I should code before opening
JH> my mouth, huh? Anycase, here is a baby implementation of a string evaluator
JH> that doesn't get hung up on integer division.
JH> ... splittext = re.split("/", text)
JH> ... text2 = ""
JH> ... for i in range(len(splittext)-1):
JH> ... text2 = text2 + splittext[i] + ".0/"
JH> ... text2 = text2 + splittext[-1]
JH> ... return eval(text2)
JH> ...
string_eval("1+2*3/4")
JH> 2.5
If you want to have fractional answers then you should also accept
fractional input. So what about
"1+2*3.6/4"
--
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl
Joal Heagney
2000-10-18 08:23:51 UTC
Permalink
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.
... 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
Joal Heagney
2000-10-18 08:01:13 UTC
Permalink
On Tue, 17 Oct 2000 04:01:24 GMT, prakash.ojha at hope.edu
I am new to Python and would like to know how can I evaluate a string
like "1+2*3/4" to give a numeric result. Is there any built in
function that will give me this result. Any help would be greatly
appreciated.
Since this is Python, the answer won't surprise you: the builtin
eval("1+2*3/4")
2
Anticipating what may be your next question, the reason that it
doesn't give you 2.5 is that in Python, division of integers produces
an integer result.
Regards,
Matt
eval("%s""1+2*3/4" 1.0)
*grins*
Joal Heagney/AncientHart
Hmm. Well that was wierd. It kinda worked on something similar, but when I
tried it on the above string to check *shrugs*. This one works, but I don't
eval("1+2*3/4"".0")
Perhaps a regular expression???

Joal Heagney/AncientHart
Matthew Dixon Cowles
2000-10-17 05:00:00 UTC
Permalink
On Tue, 17 Oct 2000 04:01:24 GMT, prakash.ojha at hope.edu
I am new to Python and would like to know how can I evaluate a string
like "1+2*3/4" to give a numeric result. Is there any built in
function that will give me this result. Any help would be greatly
appreciated.
Since this is Python, the answer won't surprise you: the builtin
eval("1+2*3/4")
2

Anticipating what may be your next question, the reason that it
doesn't give you 2.5 is that in Python, division of integers produces
an integer result.

Regards,
Matt
Joal Heagney
2000-10-18 07:54:41 UTC
Permalink
On Tue, 17 Oct 2000 04:01:24 GMT, prakash.ojha at hope.edu
I am new to Python and would like to know how can I evaluate a string
like "1+2*3/4" to give a numeric result. Is there any built in
function that will give me this result. Any help would be greatly
appreciated.
Since this is Python, the answer won't surprise you: the builtin
eval("1+2*3/4")
2
Anticipating what may be your next question, the reason that it
doesn't give you 2.5 is that in Python, division of integers produces
an integer result.
Regards,
Matt
eval("%s""1+2*3/4" 1.0)
*grins*

Joal Heagney/AncientHart

Joal Heagney
2000-10-18 19:33:39 UTC
Permalink
Post by piet
JH> Okay, sorry about that. How embarassing. Perhaps I should code before opening
JH> my mouth, huh? Anycase, here is a baby implementation of a string evaluator
JH> that doesn't get hung up on integer division.
JH> ... splittext = re.split("/", text)
JH> ... text2 = ""
JH> ... text2 = text2 + splittext[i] + ".0/"
JH> ... text2 = text2 + splittext[-1]
JH> ... return eval(text2)
JH> ...
string_eval("1+2*3/4")
JH> 2.5
If you want to have fractional answers then you should also accept
fractional input. So what about
"1+2*3.6/4"
--
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl
Yikes. *grins* How about
Post by piet
import re
... splittext = re.split("/", text)
... text2 =""
... for i in range(len(splittext) - 1):
... text2 = text2 + splittext[i] + "* 1.0/"
... text2 = text2 + splittext[-1]
... return eval(text2)
...
Post by piet
string_eval("1+2*3/4")
2.5
Post by piet
string_eval("1+2*3.6/4")
2.8

Joal Heagney/AncientHart
prakash.ojha
2000-10-17 04:01:24 UTC
Permalink
I am new to Python and would like to know how can I evaluate a string
like "1+2*3/4" to give a numeric result. Is there any built in function
that will give me this result. Any help would be greatly appreciated.


Sent via Deja.com http://www.deja.com/
Before you buy.
Continue reading on narkive:
Loading...