Python Koan 131提出异常

时间:2011-09-13 08:07:11

标签: python exception

我最近通过Python Koans学习Python,我想出了在Python中引发异常的问题。具体来说,经过公案后,我甚至对try...except...感到困惑。我知道Rudy Koan有一个类似的问题Ruby Koan 151 raising exceptions。但我是一个Python新手,对Ruby一无所知。

所以这是公案:

# You need to finish implementing triangle() in the file 'triangle.py'
from triangle import *

class AboutTriangleProject2(Koan):
    # The first assignment did not talk about how to handle errors.
    # Let's handle that part now.
    def test_illegal_triangles_throw_exceptions(self):
        # Calls triangle(0, 0, 0)
        self.assertRaises(TriangleError, triangle, 0, 0, 0)

        self.assertRaises(TriangleError, triangle, 3, 4, -5)
        self.assertRaises(TriangleError, triangle, 1, 1, 3)
        self.assertRaises(TriangleError, triangle, 2, 4, 2) 

以下是triangle.py

def triangle(a, b, c):
    # DELETE 'PASS' AND WRITE THIS CODE

    if a == b and b == c and c == a:
        return 'equilateral'
    if a == b or b == c or a == c:
        return 'isosceles'
    else:
        return 'scalene'

# Error class used in part 2.  No need to change this code.
class TriangleError(StandardError):
    pass

我应该完成triangle()功能。

根据我的理解,try...except...函数就像是满足某些条件然后做某事,否则通过例外。那么在我的情况下,我应该使用if ... then raise TriangleError还是try... except ...?它们之间有什么区别?

非常感谢!

5 个答案:

答案 0 :(得分:4)

请参阅以下答案:

def triangle(a, b, c):
    # DELETE 'PASS' AND WRITE THIS CODE
    if min([a,b,c])<=0:
        raise TriangleError
    x,y,z = sorted([a,b,c])
    if x+y<=z:
        raise TriangleError
    if a==b and b==c and c==a:
        return 'equilateral'
    if a==b or b==c or a==c:
        return 'isosceles'
    else:
        return 'scalene'    

答案 1 :(得分:1)

您希望使用raise,这将导致创建异常并向上移动调用堆栈,直到某些东西使用try / except块处理异常。例如,如果您在实际代码中使用此三角函数,则可以执行类似

的操作
try:
    t = triangle(x, y, z)
    ...
except TriangleError:
    print 'Triangle', x, y, z, 'is no good!'

这样你就可以在出现错误的三角形时处理错误并且你的程序不会崩溃。

答案 2 :(得分:1)

这是我的代码,它有效

def triangle(a, b, c):
# DELETE 'PASS' AND WRITE THIS CODE
if min([a,b,c])<=0:
    raise TriangleError,"no <0 numbers"

if sorted([a,b,c])[0]+sorted([a,b,c])[1]<=sorted([a,b,c])[2]:
    raise TriangleError,"sf"

set_a=set(([a,b,c]))
if len(set_a)==1:
    return 'equilateral'
elif len(set_a)==2:
    return 'isosceles'
elif len(set_a)==3:
    return 'scalene'

# Error class used in part 2.  No need to change this code.
class TriangleError(StandardError):
    pass

答案 3 :(得分:0)

我最后得到的东西类似于南希的答案,使用set而不是手动比较a,b和c,它也有效。

def triangle(a, b, c):
    # DELETE 'PASS' AND WRITE THIS CODE
    ls = sorted([a,b,c])
    if min(ls)<=0 or ls[0]+ls[1]<=ls[2]: raise TriangleError, 'Triángulo mal formado.'
    l = len(list(set(ls)))
    if(l==1): return 'equilateral'
    if(l==2): return 'isosceles'
    if(l==3): return 'scalene'

# Error class used in part 2.  No need to change this code.
class TriangleError(StandardError):
    pass

答案 4 :(得分:0)

triangle.py文件的更好解决方案如下:

<input type="text" name="nama_obat" value="{{ $o->nama_obat}}">

第2部分中使用的错误类。无需更改此代码。

def triangle(a, b, c):
if a <= 0 or b <= 0 or c <= 0:
    raise TriangleError(AttributeError('Negative edge is not allowed'))
if a + b + c <= 2 * max(a, b, c):
    raise TriangleError(AttributeError('largest side should be smaller than sum of 2 other sides'))
if a == b == c:
    return 'equilateral'
elif a == b or b == c or c == a:
    return 'isosceles'
return 'scalene'
相关问题