30 Day code challenge-python
Day 17–More Exception
Task
Write a Calculator class with a single method: int power(int, int). The power method takes two integers n, and p, as parameters and returns the integer result of n^p. If either n or p is negative, then the method must throw an exception with the message: n and p should be non-negative
.
Input Format
The first line contains an integer, T, the number of test cases. Each of the T subsequent lines describes a test case in 2 space-separated integers that denote n and p.
Output Format
There are lines T of output, where each line contains the result of n^p as calculated by your Calculator class’ power method.
Explanation
Line 17–22: Gets input from the user and calls the class function power.
Line 4–13: If n and p are not negative then, n^p is returned else exception block gets executed, “n and p should be non-negative” is returned
Line 23: Prints the returned output
Bonus Tip: In line 7 ‘|’ bitwise or is used instead we can write as n<0 or p<0
See you on day 18