else和elif的语法错误(无法确定结构是否存在问题)

时间:2015-07-17 08:42:46

标签: python-2.7

我是python的新手,过去几个小时我一直在使用python中的基本计算器(我知道修辞,考虑到python内置了什么,但这是我学习过程的一部分),我我遇到了一个我似乎无法修复的错误,通常我可以让我的脚本站起来并在几次Google搜索的帮助下运行,但这个让我感到难过。我得到一个语法错误,我有一个其他的,虽然起初我很确定这是一个结构问题,重写脚本没有修复任何东西,模糊我知道,所以这里的脚本(我标记了发表评论):

def Calculator():
tempnums = [] #stores nums only
tempfuncs = [] #stores funcs only
tmpfuncs = {} #stores funcs only
times = lambda multiply: tempnums[0]*tempnums[1]
div = lambda divide: tempnums[0]%tempnums[1]
plus = lambda add: tempnums[0]+tempnums[1]
minus = lambda subtract:tempnums[0]-tempnums[1]
done = 0
varnum = 0
xtimes = 0
divtimes = 0
plustimes = 0
mintimes = 0
while done == 0: #USER INPUT PROCESS
    varnum = varnum + 1
    tempint = input() #nums
    exec("num%d = tempint" % (varnum))
    function = raw_input() #functions
    if function != "=":
        if function == 'x':
            if x not in tmpfuncs:
                xtimes = xtimes + 1
                tmpfuncs[x] = times
            else:
                xtimes = xtimes + 1
                exec("tmpfuncs[x%d] = times" % (xtimes)
        else: #ERROR COMES HERE
            if function == '//':
                if dv not in tmpfuncs:
                    divtimes = divtimes + 1
                    tmpfuncs[dv] = div
                else:
                    divtimes = divtimes + 1
                    exec("tmpfuncs[dv%d] = div" % (divtimes)
            if function == '+':
                if pls not in tmpfuncs:
                    plustimes = plustimes + 1
                    tmpfuncs[pls] = plus
                else:
                    plustimes = plustimes + 1
                    exec("tmpfuncs[pls%d] = plus" % (plustimes)
            if function == '-':
                if mn not in tmpfuncs:
                    mintimes = mintimes + 1
                    tmpfuncs[mn] = minus
                else:
                    mintimes = mintimes + 1
                    exec("tmpfuncs[mn%d] = minus" % (mintimes)
    else: #user has selected to calculate input
        done = 1
        for i in range(1, varnum + 1):
            exec("tempnums.append(num%d)" % (i)) #adding nums to a list with dynamic var names
        print tmpfuncs
#next we'll make it so that tempnums[0] is the changing result as we use tempnums 0 and 1 to calculate the answer, deleting one as we go until there is only zero
        Calculator()

计算器()

我希望这是可读的,因为我是从手机发布的(事实上我也是从手机上写的)。

1 个答案:

答案 0 :(得分:0)

else上方的一行错过了一个关闭的parens:

exec("tmpfuncs[x%d] = times" % (xtimes)

应该是

exec("tmpfuncs[x%d] = times" % (xtimes))

exec行的许多其他行中也会出现同样的错误。另外,我建议您考虑重构代码,这样就根本不需要使用exec