[Tutor] infix to postfix exponent handling

Danny Yoo dyoo at hashcollision.org
Sat Aug 1 22:56:16 CEST 2015


> i also replaced the infixtopostfix to the problem:
>
> ("5 * 3 ^ (4 - 2)”))
>
> here is the error I am getting :
>
> Traceback (most recent call last):
>   File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 53, in <module>
>     print(infixToPostfix("5 * 3 ^ (4 - 2)"))
>   File "/Users/stephaniequiles/Downloads/Listings/listing_3_7.py", line 45, in infixToPostfix
>     (prec[opStack.peek()] >= prec[token]):
> KeyError: '(4'
>

Ok.  The immediate problem you are seeing here actually has nothing to do with
exponentiation, but with a lower-level issue.


Answering the following two questions should help you figure out
what's going on.

Given the string:

    "5 * 3 ^ (4 - 2)"

1.  What do you expect the list of tokens to be?  Say concretely what
you expect it to be.

2.  What does your program think the list of tokens is?


More information about the Tutor mailing list