python不会输入_输入命令将输入??作为int而不是Python中的...

我是使用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()

任何帮助,将不胜感激

我正在寻找

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值