[Tutor] Converting string or list to sum
Iain
python at cairnlin.co.uk
Thu Jan 29 15:02:37 EST 2004
On Thursday 29 January 2004 19:41, Danny Yoo wrote:
> On Thu, 29 Jan 2004, Iain wrote:
> > If I have a sum as a string (e.g. '2+3') or in a list (e.g. [2,'+',3]),
> > how can I get the result of the sum?
>
> Hi Iain,
>
>
>
> If we had a function that takes three arguments:
>
> calculate(left_hand_side, operator, right_hand_side)
>
>
> then we might be able to say something like:
>
> ###
>
> >>> calculate(2, '+', 3)
>
> 5
>
> >>> calculate(42, '-', 43)
>
> -1
> ###
>
>
> Is this the sort of thing that you're asking?
>
>
>
> Talk to you later!
Hi Danny,
I was hoping for something more generic, but that might do the trick with a
bit of effort.
I'm trying to learn Tk and writing a little GUI calculator - doubtless very
badly but never mind. I've got it doing everything except the calculation.
At the moment, the buttons pressed go into a list, so if I want 22 + 10 I'll
have a list that looks like [2,2,'+',1,0]. I want to get from that to the
answer of 32, and have it generic enough to cope with any basic arithmetic
calculation.
Of course, if this is just a terrible way to do it, perhaps someone can
suggest a better one.
Iain.
More information about the Tutor
mailing list