Skip to content

故障和錯誤

故障排除和錯誤處理

兩數相除故意輸錯

1
2
3
4
5
6
7
# 兩數相除故意輸錯

a = input('a: ')
b = input('b: ')
a = int(a)
b = int(b)
print(a / b)

例外處理語法錯誤(1)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 例外處理語法錯誤(1)

# 至少要有一個 except 區段或 finally 區段

a = input('a: ')
b = input('b: ')

try:
    a = int(a)
    b = int(b)
    print(a / b)

例外處理語法錯誤(2)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 例外處理語法錯誤(2)

# 只能有一個 finally 區段

a = input('a: ')
b = input('b: ')

try:
    a = int(a)
    b = int(b)
    print(a / b)
finally:
    print('必須輸入整數')
finally:
    print('分母不可為 0')

例外處理與函式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 例外處理與函式

import random

logs = []

def put_equation(x, n):
    logs.append(f'x + {n} = {x+n}')

def put_error():
    logs.append('error')

def print_logs():
    for line in logs:
        print(line)

try:
    while True:
        x = random.randrange(100)
        n = int(input('n: '))
        if x == 0 or n == 0:
            break
        put_equation(x, n)
except:
    put_error()
finally:
    print_logs()

知道發生某種錯誤

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 知道發生某種錯誤

a = input('a: ')
b = input('b: ')

try:
    a = int(a)
    b = int(b)
    print(a / b)
except:
    print('發生錯誤')
finally:
    print('-'*20)

知道發生什麼錯誤

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 知道發生什麼錯誤

a = input('a: ')
b = input('b: ')

try:
    a = int(a)
    b = int(b)
    print(a / b)
except ValueError:
    print('必須輸入整數')
except ZeroDivisionError:
    print('分母不得為零')
# 可以省略 finally

測試某物件是否為某類別的執行個體

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 測試某物件是否為某類別的執行個體

import unittest

class Point():
    def __init__(self):
        self.x = 5
        self.y = 5
    def __repr__(self):
        return f'({self.x}, {self.y})'

class Plane():
    def __init__(self):
        self.width = 800
        self.height = 600
    def __repr__(self):
        return f'{self.width}x{self.height}'

class TestClass(unittest.TestCase):
    def testIsInstance(self):
        point = Point()
        plane = Plane()
        print('point:', point)
        print('plane:', plane)
        # assertIsInstance(obj, cls, msg=None)
        self.assertIsInstance(point, Point)
        self.assertIsInstance(plane, Plane)
        # self.assertIsInstance(plane, Point)
        # self.assertIsInstance(point, Plane)

if __name__ == '__main__':
    unittest.main()

測試兩個物件變數是否指向同一物件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 測試兩個物件變數是否指向同一物件

import unittest

class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __repr__(self):
        return f'({self.x}, {self.y})'

class TestClass(unittest.TestCase):
    def testIs(self):
        point1 = Point(5, 5)
        point2 = Point(5, 5)
        point3 = point1
        point4 = point2
        self.assertIs(point1, point3)
        # self.assertIs(point1, point4)

if __name__ == '__main__':
    unittest.main()

測試兩個變數的值是否相同

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 測試兩個變數的值是否相同

import unittest

class Point():
    def __init__(self, x, y):
        self.position = (x, y)
    def get_postion(self):
        return self.position

class TestClass(unittest.TestCase):
    def testEqual(self):
        point1 = Point(5, 5)
        point2 = Point(5, 5)
        point3 = Point(6, 6)
        pos1 = point1.get_postion()
        pos2 = point2.get_postion()
        pos3 = point3.get_postion()
        self.assertEqual(pos1, pos2)
        # self.assertEqual(pos1, pos3)

if __name__ == '__main__':
    unittest.main()

測試串列中是否有某個值

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 測試串列中是否有某個值

import unittest

class Hundred():
    def get_odds(self, n):
        return list(range(1, n+1, 2))

    def get_evens(self, n):
        return list(range(2, n+1, 2))

class TestClass(unittest.TestCase):
    def testIn(self):
        hundredObject = Hundred()
        odds = hundredObject.get_odds(100)
        evens = hundredObject.get_evens(100)
        self.assertIn(11, odds)
        self.assertIn(21, odds)
        # self.assertIn(11, evens)
        # self.assertIn(101, odds)

if __name__ == '__main__':
    unittest.main()