Question: In Python: Postfix Calculator 1. Define a variable called expression and set it equal to an empty list 2. Prompt the user for an equation
In Python: Postfix Calculator
1. Define a variable called expression and set it equal to an empty list
2. Prompt the user for an equation to evaluate
3. Split that equation into pieces using the split() function. Example: a. test = "3 2 +" b. pieces = test.split(' ')
4. For each piece:
a. If the piece is a number (use piece.isnumeric() to figure this out):
i. Convert that piece into an INT
ii. Use the append() function to add the number to the list expression.
b. Else if the piece is an operator (e.g. +, -, *, /), it is time to perform a calculation:
i. Use the pop() function to remove the top two values from the expression list and perform the requested mathematical operation.
1. Let the right value = the first item removed from the expression list
2. Let the left value = the 2nd item removed from the expression list
ii. Use the append() function to add the result calculated in the previous step back into the expression list.
_______________________________________________________________________________________
Sample Output
*** Postfix Evaluator *** Enter an expression to evaluate: 3 2 + Result: 5 Continue (y/n)?: y Enter an expression to evaluate: 1 2 + 5 * 3 7 - / Result: -3.75 Continue (y/n)?: n
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
