<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Arnaud Delobelle wrote:
<blockquote cite="mid:m2ljezigs7.fsf@googlemail.com" type="cite">
<pre wrap="">Astan Chee <a class="moz-txt-link-rfc2396E" href="mailto:astan.chee@al.com.au"><astan.chee@al.com.au></a> writes:
</pre>
<blockquote type="cite">
<pre wrap="">Hi,
I have some variables in my script that looks like this:
vars = {'var_a':'10','var_b':'4'}
eqat = "(var_a/2.0) <= var_b"
result = "(var_a+var_b)/7"
What I'm trying to do is to plug in var_a and var_b's values from vars
into eqat and see if eqat returns true or false as well as getting the
value of result if these variables were "plugged in". How do I do
this?
I'm also expecting eqat and result to contain various python
mathematical operators like **, and compounded ()'s.
I'm not sure how to convert the equation; if I have to make a bunch of
if-statements or if there is a python function that already does
something like this.
</pre>
</blockquote>
<pre wrap=""><!---->
Yes: eval()
</pre>
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">vars = {'var_a':10 ,'var_b':4}
eqat = "(var_a/2.0) <= var_b"
result = "(var_a+var_b)/7"
eval(eqat, vars)
</pre>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->False</pre>
</blockquote>
Hi,<br>
I now have a slight problem with this. This doesnt seem to work:<br>
>>> vars = {'var a':10 ,'var b':4}<br>
>>> eqat = "(var a/2.0) <= var b"<br>
>>> eval(eqat, vars)<br>
<br>
Traceback (most recent call last):<br>
File "<pyshell#11>", line 1, in <module><br>
eval(eqat, vars)<br>
File "<string>", line 1<br>
(var a/2.0) <= var b<br>
^<br>
SyntaxError: invalid syntax<br>
<br>
So eval can't take spaces? is there a way to go around this?<br>
Thanks again for any suggestions<br>
Cheers<br>
Astan<br>
<br>
</body>
</html>