我是使用Python 2.79进行编程的初学者.我正在编写一个“标记”数学公式的程序,基本上将每个数字和运算符转换为列表中的一个项目.
我的问题是当前(在我的输入命令中)(因为这是一个语义错误,我尚未能够测试其余的代码).我要用户输入一个数学方程式. Python将此解释为int.
我尝试将其制成字符串,Python基本上解决了该公式,并通过我的tokenize函数运行该解决方案
我的代码如下:
#Turn a math formula into tokens
def token(s):
#Strip out the white space
s.replace(' ', '')
token_list = []
i = 0
#Create tokens
while i < len(s):
#tokenize the operators
if s[i] in '*/\^':
token_list.append(s[i])
#Determine if operator of negation, and tokenize
elif s[i] in '+-':
if i > 0 and s[i - 1].isdigit() or s[i - 1] == ')':
token_list.append(s[i])
else:
num = s[i]
i += 1
while i < len(s) and s[i].isdigit():
num += s[i]
i += 1
token_list.append(num)
elif s[i].isdigit():
num = ''
while i < len(s) and s[i].isdigit():
num += s[i]
i += 1
token_list.append(num)
else:
return []
return token_list
def main():
s = str(input('Enter a math equation: '))
result = token(s)
print(result)
main()
任何帮助,将不胜感激
我正在寻找