I am trying to calculate 5^2 in Python and the result is coming out as 7. I am attaching a snippet of my code. Any idea of what's happening?
I've tried:
math.pow(a,b)
a**b
eval(str(x))
I am receiving the same result of 7 for each of them. I have also imported math.
This is my current code that I am using:
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
I was originally using:
def calculate(self):
temp = 0
for i in range(len(self.problemArray)):
temp = eval(str(self.problemArray[i]))
self.answerArray.append(temp)
temp = 0