I wondered when someone would ask something like this. eval() is good and it can be done using it. I wrote a -- IMHO -- really great functiongraphing program using vpython. If you would like to see it, just reply and say so.
Pros and cons of calculating all first: pro - easier to read code con - user may feel insecure while the points are being calculated -- for example, say you type in a big, complicated function, and while the computer sits there and calculates the points, the user might feel that something is wrong. On the other hand, if you calculate each point on its own, points are immediately put on the screen so your user knows that the thing is working (if you get my drift) Please tell me what you are using to plot the points. (big grin) Vpython, wxpython, what? I'm curious--it's just someone else is working on a project that I'm working on... To help you out. You need some sort of error checking to be sure that within your given range you won't get something like a math domain error. Ex. y = 'sqrt(x)' x = -15 while -15<=x<=15: print eval(y) Gives you something like Traceback blah, blah ... Math domain error If you want more suggestions, ask.... Please, tell me how you're doing. It sounds interesting. HTH, Jacob Schmidt > Hello > > I am trying to make a program that will plot functions. For that, I need > to be able to get an input (the function to be plotted) and execute it. > So, my question is, how do I use the input? I have found no way to > convert the string to some kind of executable code. > > I did research the problem. And found two things, first, an > unsatisfactory solution from: > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52217 > The code there, by means I cannot understand at all, executes a string > as code. To be usable I have to make my code a huge string. It is not > very elegant. > > The second possible solution I found was using eval, compile and/or > exec. But I do not understand what do they do, or how to use them, for > the matter. > > Related to the program I intend to do, which design would you say is > more intelligent: one that first calculates the whole function, stores > it, and then plots it; or one that calculates-plots-calc.-plot, and so on? > > Thanks for any information, and for your time. > Ismael > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor