Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calculator app problem
#2
The most common way I've seen is to make a tkinter calculator is use the buttons to enter an expression, and and eval() to evaluate the expression and display the result. In this type of calculator you don't worry about numbers or operators, the key presses just add a character to the expression text.
3 - 6 / 2 would be one string. Pressing "=" would call eval("3 - 6 / 2") and paste the result in the display.

If you want a calculator that works more like a regular calculator, I suggest a postfix calculator. In a postfix calculator you would enter the expression 3 - 6 / 2 as 3 6 2 / -. This calculator has no "=" button, it has an enter button that you press when you are done entering a number. To enter the equation 3 - 6 / 2 you would press 3 enter 6 enter 2 / -. The advantage of postfix is you don't have to worry about operator precedence, because equations are evaluated as soon as an operator key is pressed. The disadvantage is most people find postfix confusing. Once you get used to it, you never want to go back to infix.

An infix calculator is challenging. Your code needs to know operator precedence and perform calculations differently based on the precedence.
You will not have first_num and second_num, but instead have a stack of nums. Solving 3 - 6 / 2 requires holding on to the 3 until after you divide 6 / 2, then you can do the subtraction. If done properly, there is no need for a continue with result checkbox.

The reason your code does not reset separated = False after pressing the enter key is because the separated variable is local to the equals() function. It is not the global separated variable used in concatenate_digit(). Also, you don't assign anything to first_num or second_num in concatenate_digit(), so these don't need to be declared as global.
Reply


Messages In This Thread
calculator app problem - by jacksfrustration - Apr-11-2024, 12:13 PM
RE: calculator app problem - by deanhystad - Apr-11-2024, 03:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculator problem BeginnerCoderPaero 4 3,479 Jun-29-2018, 09:21 AM
Last Post: BeginnerCoderPaero
  Beginner. Calculator problem ¯\_(ツ)_/¯ stykus1992 0 2,436 Feb-15-2018, 11:01 AM
Last Post: stykus1992
  Tip calculator problem Dhaval 1 4,538 Jun-06-2017, 12:49 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020