# [Tutor] Problems with Calculator Program

Luke Paireepinart rabidpoobear at gmail.com
Thu Oct 30 17:22:09 CET 2008

```Seems like when they press "op" you assume that the current number is
done being executed and save the value.  Then when they press another
op, you evaluate the previous op with the new value.  You don't update
the display until they start typing again.
However, when they press =, you evaluate the previous instruction,
display the result, and then accept input.

On Thu, Oct 30, 2008 at 10:50 AM, Richard Lovely
> Hi,  I'm trying to teach myself Tkinter by making a desktop calculator.
>
> I've got the Tkinter bit working, but getting the output to work how I
> want it to is causing serious brainfreeze...
>
> The buttons 0 through 9 and '.' all append to a queue, which
> makeNumber(queue) returns as an actual python number.  That all works.
>
> Pressing one of the operators (+, -, /, *, or =) calls the following function:
>
> def evaluate(op):
>    """Evaluate a queue of button presses:
>    eg: [1, 2, 3, '+', 3, 2, 1] => 123 + 321 = 444"""
>
>    global last_op, total
>
>    initial_total = total
>    try:
>        operand = makeNumber(queue)
>    except ValueError:
>        operand = ''
>
>        disp_op = '+'
>        total += operand if operand != '' else 0
>
>    elif last_op == 'sub':
>        disp_op = '-'
>        total -= operand if operand != '' else 0
>
>    elif last_op == 'div':
>        disp_op = '/'
>        total /= float(operand) if operand != '' else 1
>
>    elif last_op == 'mul':
>        disp_op = '*'
>        total *= operand if operand != '' else 1
>
>    else:
>        disp_op = ''
>    if int(total) == total:
>        total = int(total)
>
>    if last_op == '':
>        print operand,
>    else:
>        print initial_total, disp_op, operand, '=', total
>    last_op = op
>    clearqueue()
>
> What I want it to do is something like:
> (the first list indicates button presses NOT the contents of the queue
> [1, 2, 3, '+', 3, 2, 1, '*' 2] =>
> queue = [1,2,3]; evaluate('add') gives output:
> 123 + 321 = 444
> queue = [3,2,1]; evaluate('mul') gives output:
> 444 * 2 = 888
>
> Like I said, the makeNumber function works, and the numbers are
> definatly getting appended to the queue.  The logic within the above
> function is the problem.  The main problem is allowing button
> sequences like:
> number op number equals, number op number equals
>
>
> Did that make any sense at all?
> --
> Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
> www.theJNP.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
```