You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python代码报错TypeError: 'int' object is not callable求助

TypeError: 'int' object is not callable 计算表达式"3(5+3)"时出错

我编写了一个Python高级计算器代码,目标是解析并计算像3(5+3)这种省略乘号的表达式,但运行时抛出TypeError: 'int' object is not callable错误。尝试过str()、map()方法,查阅文档和咨询工具都没能解决问题,相关代码如下:

print ("____________________________")
print ("Advanced calculator selected")
print ("Experimental! May break.")
print ("NOTE: Does not work with variables... yet.")
print ("Key:", "\"()\" mean parenthesis", "\"^\" mean exponent", "\"*\" means multiplication", "\"/\" means division", "\"+\" means addition", "\"-\" means subtraction", sep="\n  ")
option = "3(5+3)" #input("Equation to solve:").strip()
option2 = list(option)
count = -1
c = True
while count < len(option2) - 1:
    if c is True:
        count += 1
        c = False
    elif c is False:
        c = True
    if option2[count] == " ":
        option2.pop(count)
        count -= 1
        continue
    elif option2[count] in [0,1,2,3,4,5,6,7,8,9] and option2[count + 1] == "(":
        option2.insert(count + 1, "*")
        count += 1
    elif option2[count] in ["(", ")", "*", "/", "+", "-"]:
        continue
    elif option2[count] == "^":
        option2.pop(count)
        option2.insert(count, "**")
    elif str(option2[count]).isalpha() is True:
        print ("Option not recognized, restarting")
        counting.Calculator.Advanced()
    else:
        continue
print (option2)
answer = "".join(option2)
print (answer.isascii())
answer = eval(str(answer))
print (answer)
input()

错误原因与修复方案

核心问题

代码未正确识别数字与括号间的省略乘号,导致最终传给eval()的表达式仍是3(5+3)。在Python中,3(...)会被解析为“调用值为3的函数”,但整数3不是可调用对象,因此抛出TypeError。

问题出在数字判断逻辑:

elif option2[count] in [0,1,2,3,4,5,6,7,8,9] and option2[count + 1] == "(":

option2是字符串拆分后的字符列表,每个元素都是字符串类型(比如"3"),但你判断它是否在整数列表[0,1,...9]中,这个条件永远不成立,因此不会插入*号。

修复步骤

  1. 修正数字判断逻辑:
    将整数列表改为字符串列表,或直接用isdigit()方法更简洁:

    # 写法1:字符串列表判断
    elif option2[count] in ["0","1","2","3","4","5","6","7","8","9"] and option2[count + 1] == "(":
    # 写法2:更简洁的isdigit()判断
    elif option2[count].isdigit() and option2[count + 1] == "(":
    
  2. 优化循环逻辑:
    原代码中c变量的切换会导致跳过部分字符,可能漏处理表达式。可以简化为更直观的遍历逻辑:

    count = 0
    while count < len(option2):
        if option2[count] == " ":
            option2.pop(count)
            continue
        # 增加边界判断,避免索引越界
        elif option2[count].isdigit() and count + 1 < len(option2) and option2[count + 1] == "(":
            option2.insert(count + 1, "*")
            count += 1  # 跳过插入的*,避免重复处理
        elif option2[count] == "^":
            option2[count] = "**"  # 直接替换,比pop+insert更高效
        elif option2[count].isalpha():
            print("Option not recognized, restarting")
            counting.Calculator.Advanced()
        count += 1
    

修复后效果

处理"3(5+3)"时,会正确插入*号变成"3*(5+3)",eval()可计算出正确结果24。

内容的提问来源于stack exchange,提问作者Chief Guy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 05:35:07