We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def power(self,num1,num2):
if num1 >= 0 and num2 >= 0 :
return num1**num2
elif num1 >= 0 and num2 <= 0:
return "n and p should be non-negative"
elif num1 <= 0 and num2 >= 0:
return "n and p should be non-negative"
else:
return "n and p should be non-negative"
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)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 17: More Exceptions
You are viewing a single comment's thread. Return to all comments →
python3
import math
class Calculator: def init(self): pass
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)