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.')
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') else: # 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()) try: ans = cl1.power(n,p) print(ans) except Exception as e: print(e) ''' Sample Input 4 3 5 2 4 -1 -2 -1 3 Sample Output 243 16 n and p should be non-negative n and p should be non-negative '''
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.