Hello, I wrote the following in python that parses mathematical expressions and I'm trying to store the results in an array. For example, if I enter 3+42, the array should be ['3', '4','','2']. Unfortunately, I get the following:
['3']
['4']
['*']
['2']
Can anyone help me figure out what is wrong? The tokenizer seems to work fine. Just can't append to the array. I'd also like to add the word 'end' at the end of the array so that my ideal output would be ['3', '4','*','2','end']
from tokenizer import *
while get_expression():
t = get_next_token()
while t:
if str.isdigit( t[0] ) : # we have a (non-negative) number
op = 'operand'
else:
op = 'operator'
li = []
li.append(t)
print li
t = get_next_token()
print ''
---------- Post updated at 10:42 PM ---------- Previous update was at 10:22 PM ----------
hello everyone,
I was able to append the array correctly by doing the following:
#!/usr/bin/env python
from tokenizer import *
while get_expression():
t = get_next_token()
li = []
while t:
if str.isdigit( t[0] ) : # we have a (non-negative) number
op = 'operand'
else:
op = 'operator'
li.append(t)
t = get_next_token()
print li
print ''
However, I'm still having a hard time adding the string 'end' to the end of the array. Any suggestions?