Question: (Python pls) i need help with my code when i run the code it will print out a double parenthesis sign in an equation with

(Python pls) i need help with my code when i run the code it will print out a double parenthesis sign in an equation with parenthesis and the double addition sign.

def infixToPostfix(infixexpr): prec = {} prec["*"] = 3 prec["/"] = 3 prec["+"] = 2 prec["-"] = 2 prec["("] = 1 prec[")"] = 1 opStack = Stack() opStack = Stack() postfixList = [] tokenList = infixexpr.split() for token in tokenList: if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789": postfixList.append(token) elif token == ')': topToken = opStack.pop() while topToken !='(': postfixList.append(topToken) topToken = opStack.pop() else: while( not opStack.isEmpty()and(prec[opStack.peek()])>=prec[token]): postfixList.append(opStack.pop()) opStack.push(token) while not opStack.isEmpty(): postfixList.append(opStack.pop()) return " ".join((postfixList)) print(infixToPostfix("A B + C D")) def postfix_evaluation(s): s=s.split() s.reverse() eqtn = Stack() for items in s: eqtn.push(items) s.reverse() print(s) for element in s: if element.isdigit(): print(element,"is a constant") elif element.isalpha(): print(element,"is a variable") elif element == "+": print(element," is addition operator") elif element== "-": print(element," is subraction operator") elif element == "(": print(element," is parenthesis operator") elif element == ")": print(element," is parenthesis operator") elif element == "*": print(element," is multiplication operator") elif element == "/": print(element," is divison operator") n=len(s) stack=[] for i in range(n): x = eqtn.peek() if s[i].isdigit(): x = 1 #append function is equivalent to push #stack.append(int(s[i])) elif s[i].isalpha(): x = 1 elif s[i]=="+": if len(stack) != 0: if stack[len(stack)-1].isdigit() and s[i+1].isdigit(): a = stack.pop() b = s[i + 1] x = int(a) + int(b) stack.append(str(x)) else: a = stack.pop() b = s[i + 1] c = a + ' + ' + b stack.append(c) else: if s[i-1].isdigit() and s[i+1].isdigit(): a = s[i - 1] b = s[i + 1] stack.append(int(a)+int(b)) else: c = a + ' + ' + b stack.append(c) elif s[i]=="*": if len(stack) != 0: a = stack.pop() b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append(int(a) * int(b)) else: c = a + ' * ' + b stack.append(c) else: a = s[i - 1] b = s[i + 1] if a.isdigit() and b.isdigit(): x = int(a) * int(b) stack.append(str(x)) else: c = a + ' * ' + b stack.append(c) elif s[i]=="/": if len(stack) != 0: a = stack.pop() b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append(int(a) / int(b)) else: c = a + ' / ' + b stack.append(c) else: a = s[i - 1] b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append(int(a) / int(b)) else: c = a + ' / ' + b stack.append(c) elif s[i]=="-": if len(stack) != 0: a = stack.pop() b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append(int(a) - int(b)) else: c = a + ' - ' + b stack.append(c) else: a = s[i - 1] b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append(int(a) - int(b)) else: c = a + ' - ' + b stack.append(c) elif s[i]=="(": if len(stack) != 0: a = stack.pop() b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append(int(a) - int(b)) else: c = a + ' ( ' + b stack.append(c) else: a = s[i - 1] b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append(int(a) - int(b)) else: c = a + ' ( ' + b stack.append(c) elif s[i]==")": if len(stack) != 0: a = stack.pop() b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append() stack.append(int(a) - int(b)) else: c = a + ' ) ' + b stack.append(c) else: a = s[i - 1] b = s[i + 1] if a.isdigit() and b.isdigit(): stack.append(int(a) - int(b)) else: c = a + ' ) ' + b stack.append(c) print(stack) return stack.pop() s = input("Equation:") #put spaces in equation example: 10 * ( 4 ) + 2 val = postfix_evaluation(s) print(val) 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!