Py学习  »  Python

这是用python编写异常的正确方法吗?

anjali pandey • 5 年前 • 1905 次点击  

以下代码是用Python编写异常的正确方法吗?

class Calculator:
    def power(self,n,p):
        self.n=n
        self.p=p
        if self.n>=0 and self.p>=0:
            return self.n**self.p
        else:
            return ("n and p should be non-negative")


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)   

谢谢!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51710
 
1905 次点击  
文章 [ 1 ]  |  最新文章 5 年前