I’m working on developing a mini-language in Python (not really, just a few commands for a personal project).
Here’s the code:
class FlashCard:
def __init__(self):
self.commands = {'addQuestion':self.addQuestion}
self.stack = []
self.questions = {}
def addQuestion(self):
question = self.stack.pop()
answer = input(question)
def interpret(self,expression):
for token in expression.split():
if token in self.commands:
operator = self.commands[token]
operator()
else:
self.stack.append(token)
i = FlashCard()
i.interpret('testing this addQuestion')
The interpret function will only pull the last word (this) from the string. Is there a way to make it pull the entire line?
Thank you!
Since stack is a list, and you are calling the
popmethod without arguments, what you will get is the last elements in the list. You probably want to transform the list in a space-separated string, instead:Observe that the side effect of
popandjoinare different.popwill modify the original list:while
joinwon’t:Edit (see comments of OP below): To parse multiple lines / commands in the same input, you could do different things. The easiest that comes to my mind: flush the stack after the call to
operator():Edit 2 (see my own comment below): Here’s the complete example using a list of strings:
HTH!