Reputation: 381
My task is to code some functions to evaluate postfix, prefix and infix expressions. This is fine and dandy, and I've finished that portion of the task, however my problem arises when I take in the expression. If the user inputs a string lead by 'V', I have to assign the integers that come after 'V' to as many variables as needed. To try and be more clear, if the input was 'V 1 2 4 9', I would have to assign 1 to the letter A, 2 to the letter B, 4 to the letter C, and 9 to the letter D. I can do this if I assign each, variable by variable. But that is a long and arduous process, and I'm sure there's a better way to do it. The reason I need to assign the integer values to variables is because when the user wants to evaluate an expression, it has to be in the form '-+AB*CF', for example, if it was a prefix notation expression. Of course the form changes if the users wants infix or postfix, but the values to be evaluated are still in alphabet form, much like mathematics I guess.
My apologies if this question in unclear. Obviously this is homework, however I don't need code or people to do it for me, just some clarification about the best method would more than suffice. Thanks all.
Upvotes: 0
Views: 496
Reputation: 1121992
Store the values in a dictionary instead:
import string
keys = string.ascii_uppercase
values = # make list of the values
variables = {keys[i]: val for i, val in enumerate(values)}
Now you can address the values in V 1 3 4
as variables['A']
, variables['B']
, etc.
Upvotes: 4