你只是从
power
你可能想提出一个例外。另外,你应该检查一下
n
和
p
之前
修改对象。(我不想进一步解释为什么
权力
正在设置属性。)
class Calculator:
def power(self, n, p):
if n < 0 or p < 0:
raise ValueError("Both arguments should be non-negative")
self.n = n
self.p = p
return self.n ** self.p
myCalculator = Calculator()
T = int(input())
for i in range(T):
n, p = map(int, input().split())
try:
ans = myCalculator.power(n,p)
print(ans)
except Exception as e:
print(e)