Re: Transforming a str to an operator

2009-08-29 Thread r
On Aug 28, 8:43 pm, Anny Mous b1540...@tyldd.com wrote: It isn't irrational to have a healthy caution towards eval. Ignorance is never an excuse for stupidity. No caution is needed if you know how to properly use eval. You can't shoot yourself in the foot without first pulling the trigger.

Re: Transforming a str to an operator

2009-08-29 Thread MRAB
r wrote: On Aug 28, 8:43 pm, Anny Mous b1540...@tyldd.com wrote: It isn't irrational to have a healthy caution towards eval. Ignorance is never an excuse for stupidity. No caution is needed if you know how to properly use eval. You can't shoot yourself in the foot without first pulling the

Re: Transforming a str to an operator

2009-08-28 Thread Stephen Hansen
I would use the following approach: Abviously the OP is a python baby noob and casting your irrational fear (and many others irrational fears) of eval at him is akin to tales of Chupacabras running a muck in the jungle sucking the blood from live goats in the twilight hours. I use eval all

Re: Transforming a str to an operator

2009-08-28 Thread Duke Normandin
On Thu, 27 Aug 2009, Stephen Hansen wrote: num1 = raw_input('Enter the first number: ') num2 = raw_input('Enter the second number: ') op = raw_input('Select one of the following [+-*/]: ') print 'The answer is: ', int(num1), eval(op), int(num2)

Re: Transforming a str to an operator

2009-08-28 Thread Duke Normandin
On Thu, 27 Aug 2009, r wrote: On Aug 27, 10:52 pm, Duke Normandin dukeofp...@ml1.net wrote: How do I convert the contents of op from a string to an actual arithmetic operator? eval() does not seem to be the answer. TIA! Try this.. op = '+' one = '1' two = '2' one+op+two '1+2'

Re: Transforming a str to an operator

2009-08-28 Thread Duke Normandin
On Fri, 28 Aug 2009, Ben Finney wrote: Duke Normandin dukeofp...@ml1.net writes: Hey I'm a Python noob So far so good! I've written the following: num1 = raw_input('Enter the first number: ') num2 = raw_input('Enter the second number: ') op = raw_input('Select one

Re: Transforming a str to an operator

2009-08-28 Thread Gabriel Genellina
En Fri, 28 Aug 2009 01:50:37 -0300, Xavier Ho cont...@xavierho.com escribió: On Fri, Aug 28, 2009 at 2:35 PM, Ben Finney ben+pyt...@benfinney.id.auben%2bpyt...@benfinney.id.au wrote: op_funcs = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/':

Re: Transforming a str to an operator

2009-08-28 Thread Anny Mous
r wrote: Abviously the OP is a python baby noob and casting your irrational fear (and many others irrational fears) of eval It isn't irrational to have a healthy caution towards eval. Apart from the security issues, running code in eval takes a massive performance hit. Its about ten times

Transforming a str to an operator

2009-08-27 Thread Duke Normandin
Hey I'm a Python noob So far so good! I've written the following: num1 = raw_input('Enter the first number: ') num2 = raw_input('Enter the second number: ') op = raw_input('Select one of the following [+-*/]: ') print 'The answer is: ', int(num1), eval(op), int(num2)

Re: Transforming a str to an operator

2009-08-27 Thread Xavier Ho
On Fri, Aug 28, 2009 at 1:52 PM, Duke Normandin dukeofp...@ml1.net wrote: How do I convert the contents of op from a string to an actual arithmetic operator? eval() does not seem to be the answer. TIA! Maybe you were looking for print eval(num1 + op + num2) # it's a little ugly string

Re: Transforming a str to an operator

2009-08-27 Thread r
On Aug 27, 10:52 pm, Duke Normandin dukeofp...@ml1.net wrote: How do I convert the contents of op from a string to an actual arithmetic operator? eval() does not seem to be the answer. TIA! Try this.. op = '+' one = '1' two = '2' one+op+two '1+2' eval(one+op+two) 3 you could also use

Re: Transforming a str to an operator

2009-08-27 Thread Stephen Hansen
num1 = raw_input('Enter the first number: ') num2 = raw_input('Enter the second number: ') op = raw_input('Select one of the following [+-*/]: ') print 'The answer is: ', int(num1), eval(op), int(num2) How do I convert the contents of op from a

Re: Transforming a str to an operator

2009-08-27 Thread Ben Finney
Duke Normandin dukeofp...@ml1.net writes: Hey I'm a Python noob So far so good! I've written the following: num1 = raw_input('Enter the first number: ') num2 = raw_input('Enter the second number: ') op = raw_input('Select one of the following [+-*/]: ') print 'The answer is:

Re: Transforming a str to an operator

2009-08-27 Thread Xavier Ho
On Fri, Aug 28, 2009 at 2:35 PM, Ben Finney ben+pyt...@benfinney.id.auben%2bpyt...@benfinney.id.au wrote: import operator op_funcs = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div, } num_1 = int(raw_input('Enter

Re: Transforming a str to an operator

2009-08-27 Thread r
On Aug 27, 11:35 pm, Ben Finney ben+pyt...@benfinney.id.au wrote: In general, ‘eval’ on unsanitised input is not the answer. Yes i agree. I would use the following approach: Abviously the OP is a python baby noob and casting your irrational fear (and many others irrational fears) of eval at