Day 17: More Exceptions

  • Instructions:
    • Write a Calculator class with a single method: int power(int,int)
    • The power method takes two integers n & 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
    Excercise
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    #Write your code here
    
    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) 
    
    Solution
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    #!/bin/python3
    
    #Write your code here
    
    class Calculator:
        def power(self,n,p):
            Error = ValueError('n and p should be non-negative')
            self.n = n
            self.p = p
            if self.n < 0 or self.p < 0:
                raise Error
            else:
                return n ** 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)