Python 接收到5^2的错误结果

Python 接收到5^2的错误结果,python,math,Python,Math,我试图用Python计算5^2,结果是7。我正在附加一段代码。知道发生了什么吗 我试过: math.pow(a,b) a**b eval(str(x)) 我收到的结果是相同的,每个都是7。我还引进了数学 这是我当前使用的代码: question = ["5^2"] def checkExponent(self, question): for i in range(len(question)): if question[i] == "^

我试图用Python计算5^2,结果是7。我正在附加一段代码。知道发生了什么吗

我试过:

math.pow(a,b)
a**b
eval(str(x))
我收到的结果是相同的,每个都是7。我还引进了数学

这是我当前使用的代码:

question = ["5^2"]
def checkExponent(self, question):
     for i in range(len(question)):
          if question[i] == "^":
               return(math.pow(int(question[i-1]), int(question[i+1])))

def calculate(self):
     temp = 0
     for i in range(len(self.problemArray)):
          if self.checkExponent(self.problemArray[i]) == "":
               temp = self.checkExponent(self.problemArray[i])
          else:
               temp = eval(str(self.problemArray[i]))
          self.answerArray.append(temp)
          temp = 0
我最初使用的是:

def calculate(self):
     temp = 0
     for i in range(len(self.problemArray)):
          temp = eval(str(self.problemArray[i]))
          self.answerArray.append(temp)
          temp = 0

Python充满了程序员可以使用的惊人之处 为了计算5^2,只需编写
5**2
我不明白你为什么坚持用这么长的代码来完成这么简单的操作。
我承认这不是您问题的完美答案,尽管这是一种更优化的方法,对您有益

^
按位异或运算符。 如果求值(),则
5^2
变为
0b101 xor 0b010
因此
0b111
=7


使用
a**b
,避免使用eval.

这是一种异或位运算符。请用完整的代码更新您的问题。您似乎发布了一个类中的一些方法,但没有调用这些方法的示例。无法重现此问题<代码>检查指数(无,问题[0])->
25.0
。你需要提供一个新的解决方案。编辑:哦,等等,这是在转移注意力吗?我错过了你调用的代码
eval
,即
eval(“5^2”)
->
7
。重复:,好的,谢谢。我将有问题的文件从“^”改为“**”,现在它可以正常运行了。