Task - Exception Handling - More Exceptions #16
Replies: 10 comments
-
class Calculator:
def power(self, a,b):
if b < 0:
raise Exception("Cannot power, positive values only")
else:
print(pow(a,b))
calc = Calculator()
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
try:
print(calc.power(a,b))
except Exception as e:
print(e) |
Beta Was this translation helpful? Give feedback.
-
class Calculator:
def power(self, n, p):
if n < 0 or p < 0:
raise Exception("Number and Power should not be non-negative")
else:
return n**p
myCalculator=Calculator()
T=int(input("Enter the number of operations to calculate: "))
for i in range(T):
n,p = map(int, input("Enter the number in the format [num] [pow]: ").split())
try:
ans=myCalculator.power(n,p)
print(ans)
except Exception as e:
print(e) |
Beta Was this translation helpful? Give feedback.
-
Calculator Exception# create a class
class Calculator():
def power(n,p):
if n < 0 or p < 0:
raise Exception("n and p should be non-negative")
else:
return n**p
# given code
if __name__ == "__main__":
myCalculator=Calculator()
T=int(input("Enter the total operations: "))
for i in range(T):
n,p = map(int, input("Enter numbers to operate n^p (separated by a space 'n p'): ").split())
try:
ans=myCalculator.power(n,p)
print(ans)
except Exception as e:
print(e) |
Beta Was this translation helpful? Give feedback.
-
import math
#Write your code here
class Calculator:
def power(self, n,p):
if p < 0:
raise Exception("Power cannot be negative")
else:
print(math.pow(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) |
Beta Was this translation helpful? Give feedback.
-
class Calculator():
def power(self, n, p):
if n < 0 or p < 0:
raise Exception("Number must be positive")
else:
return n ** p
if __name__ == "__main__":
myCalculator=Calculator()
T=int(input("Enter the total of number to raise: "))
for i in range(T):
n,p = map(int, input("Enter values separeted by a space: ").split())
try:
ans=myCalculator.power(n,p)
print(f"The result of {n} to the power of {p} is {ans}")
except Exception as e:
print(e) |
Beta Was this translation helpful? Give feedback.
-
Calculator Exceptionclass Calculator:
def power(self,n,p):
if n < 0 or p < 0:
raise Exception("n and p should be non-negative")
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) |
Beta Was this translation helpful? Give feedback.
-
class Calculator:
def power(self, a,b):
if b < 0:
raise Exception("Cannot power, positive values only")
else:
print(pow(a,b))
calc = Calculator()
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
try:
print(calc.power(a,b))
except Exception as e:
print(e)`` |
Beta Was this translation helpful? Give feedback.
-
class Calculator():
def power(self, n, p):
if n < 0 or p < 0:
raise Exception("n or p should not be negative")
else:
return pow(n, p)
myCalculator=Calculator()
T=int(input())
for i in range(T):
n,p = map(int, input().split())
try:
print(n, p)
ans=myCalculator.power(n,p)
print(ans)
except Exception as e:
print(e) |
Beta Was this translation helpful? Give feedback.
-
import math
Calculator:
def power(self, n,p):
if p < 0:
raise Exception("Power cannot be negative")
else:
print(math.pow(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) |
Beta Was this translation helpful? Give feedback.
-
1.- class Calculator:
def power(self, n, p):
if n < 0 or p < 0:
raise Exception('n and p should be non-negative')
else:
return n**p
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) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
More Exceptions
Problem
Objective
The previous challenge #15 taught you to manage exceptional situations by using try and catch blocks. In today's challenge, you're going to practice throwing and propagating an exception.
Task
Write a
Calculator
class with a single method:int power(int,int)
. The power method takes two integersn
andp
, as parameters and returns the integer result of np. If eithern
orp
is negative, then the method must throw an exception with the message :n and p should be non-negative
.Note : Do not use an access modifier (e.g.: public) in the declaration for your
Calculator
class.Input Format
Input from stdin is handled for you by the locked stub code in your code editor. The first line contains an integer,
T
, the number of test cases. Each ofT
subsequent lines describes a test case in 2 space-separated integers denotingn
andp
, respectively.Constraints
No Test Case will result in overflow for correctly written code.
Output Format
Output to stdout is handled for you by the locked stub code in your editor. There are
T
lines of output, where each line contains the result of np as calculated by yourCalculator
class' power method.Sample Input
Sample Output
Explanation
T = 4
T0 :
3
and5
are positive so power returns the result of 33 which is243
T1 :
2
and4
are positive so power returns the result of 24 which is16
T2 : Both inputs,
-1
and-2
are negative, so power throws an exceptionn and p should be non-negative
is printed.T3 : One of the inputs
-1
is negative, so power throws an exceptionn and p should be non-negative
is printed.Given code
Beta Was this translation helpful? Give feedback.
All reactions