Python Forum
Help with try, except, else finally
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with try, except, else finally
#6
Use python tags for code.

Yes, that code looks better.

You are using Polish notation, developed by the Polish mathematician Jan Łukasiewicz. Most calculators of the past used Reverse Polish notation (RPN), so you would type in the two values and then provide the operator. Note that PN and RPN are both parenthesis-free. So instead of prompting for the operator, and then the two operands, you could do like old mechanical calculators did: ask for the values, and then the operator. However, you could also use a single line of input, e.g.


1 2 +
but, even better
1 2 + 3 * 4 /
This involves having a stack; when you encounter an integer, you push it onto the stack, when you encounter an operator, you pop off the top two stack elements, do the computation, and push them back on. The above expression computes
(1 + 2)*3 / 4
whereas
1 2 + 3 * 3 1 + /
computes
(1 + 2) * 3 / (3 + 1)
and works like this
1 2 + 3 * 3 1 + /
 ^
 |
 you are here
stack:
1

1 2 + 3 * 3 1 + /
   ^
stack:
1 2

1 2 + 3 * 3 1 + /
     ^
stack:
3
1 2 + 3 * 3 1 + /
       ^
stack:
3 3

1 2 + 3 * 3 1 + /
         ^
stack:
9
1 2 + 3 * 3 1 + /
           ^
stack:
9 3

1 2 + 3 * 3 1 + /
             ^
stack:
9 3 1

1 2 + 3 * 3 1 + /
               ^
Stack:
9 4

1 2 + 3 * 3 1 + /
                 ^
Stack:
2
Now you have invented the HP calculator. However, there are a couple very simple algorithms for converting infix (with parentheses or not) using standard operator hierarchy, but I don't want to go into that until you have chosen to implement RPN or not.
Reply


Messages In This Thread
Help with try, except, else finally - by Pytho13 - Mar-21-2021, 04:17 PM
RE: Help with try, except, else finally - by supuflounder - Mar-22-2021, 09:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I a retard - else and finally blocks in a try statement RubenF85 6 3,930 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  finally clause Skaperen 6 5,201 Jun-02-2019, 09:02 PM
Last Post: snippsat
  try, except, finally ? microphone_head 3 3,788 Apr-28-2019, 09:36 PM
Last Post: microphone_head

Forum Jump:

User Panel Messages

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