代码显示“TypeError:'int'对象不可调用”

问题描述 投票:0回答:1

这是代码:

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()

我不明白为什么这么说。它应该只是在

option
中吐出这个方程 作为一个数字,但它却给出了错误。

我尝试过使用

str()
,我尝试过
map()
。我不知道出了什么问题,ChatGPT 也没有帮助,该功能的文档也没有帮助。

python python-3.x eval
1个回答
0
投票

option2
的每个元素都是一个字符串。这意味着这个说法永远不会是真的:

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

相反,您想知道字符串是否包含 ASCII 数字:

                elif option2[count].isdigit() and option2[count + 1] == "(":
                    option2.insert(count + 1, "*")
                    count += 1

顺便说一句,你的“计数”

© www.soinside.com 2019 - 2024. All rights reserved.