DEV Community


Posted on



It's been a while since I posted anything about my progress and I will give my explanation right after explaining what I understood from this challenge.

The challenge was about raising an Exception.Raising an exception is when one forces an exception to occur, it’s identified by the keyword raise followed by an exception instance or exception class then add a message to be displayed when the exception occurs. E.g.

raise Exception('I forced this.')
Enter fullscreen mode Exit fullscreen mode

The 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.


class Calculator:
    def power(self,n,p):
        #check if either n or p is negative
        if n < 0 or p < 0:
            #raising the exception if either of the values is negative
            raise Exception('n and p should be non-negative')
            # returning the value of n to the power of p
            return n ** p

cl1 = Calculator()
T = int(input()) #number of test cases to be input
#looping though every test case
for i in range(T):
    #create two variables as the input is space-separated
    n, p = map(int, input().split())
        ans = cl1.power(n,p)
    except Exception as e:

Sample Input

3 5
2 4
-1 -2
-1 3
Sample Output

n and p should be non-negative
n and p should be non-negative
Enter fullscreen mode Exit fullscreen mode

I learnt a new concept by this and I look forward to implementing it in my code.

Aside from this, I have to admit that currently I should be in Day 28 of the challenge but I have struggled with some of the questions, some I know bits others I have no clue so this will be my last challenge to post because I have no clear understanding of the others.

I am still learning them but recently I got a work opportunity which takes most of my time now and uses a different programming language so that means i have to get familiar with that language as well. I'm well aware that learning two or more languages consecutively will be a challenge but I will try to practice even if it's 15 minutes for some days. I will post every bit of new information I learn.

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.