Kiểm thử chương trình

Thông báo lỗi

In [23]:
test = [1, 2, 3]
test[4]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-23-e05c7705baba> in <module>()
      1 test = [1, 2, 3]
----> 2 test[4]

IndexError: list index out of range
In [24]:
int(test)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-9411534eab2d> in <module>()
      1 
----> 2 int(test)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
In [25]:
a
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-25-3f786850e387> in <module>()
----> 1 a

NameError: name 'a' is not defined
In [26]:
'3'/4
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-9d0caf584989> in <module>()
----> 1 '3'/4

TypeError: unsupported operand type(s) for /: 'str' and 'int'
In [28]:
a = len([1, 2, 3]
print(a)
  File "<ipython-input-28-2faa178a2771>", line 2
    print(a)
        ^
SyntaxError: invalid syntax

Ví dụ

In [2]:
def isPal(x):
    assert type(x) == list
    temp = x
    temp.reverse
    if temp == x:
        return True
    else:
        return False

def silly(n):
    for i in range(n):
        result = []
        elem = input('Enter element: ')
        result.append(elem)
    if isPal(result):
        print('Yes')
    else:
        print('No')
In [3]:
silly(5)
Enter element: a
Enter element: b
Enter element: c
Enter element: b
Enter element: a
Yes
In [4]:
silly(10)
Enter element: p
Enter element: a
Enter element: l
Enter element: i
Enter element: n
Enter element: n
Enter element: i
Enter element: l
Enter element: a
Enter element: p
Yes
In [5]:
silly(2)
Enter element: a
Enter element: b
Yes
In [6]:
def isPal(x):
    assert type(x) == list
    temp = x
    temp.reverse
    if temp == x:
        return True
    else:
        return False

def silly(n):
    for i in range(n):
        result = []
        elem = input('Enter element: ')
        result.append(elem)
    print(result)
    if isPal(result):
        print('Yes')
    else:
        print('No')
In [7]:
silly(2)
Enter element: a
Enter element: b
['b']
Yes
In [8]:
def isPal(x):
    assert type(x) == list
    temp = x
    temp.reverse
    if temp == x:
        return True
    else:
        return False

def silly(n):
    for i in range(n):
        result = []
        elem = input('Enter element: ')
        result.append(elem)
        print(result)
    if isPal(result):
        print('Yes')
    else:
        print('No')
In [9]:
silly(2)
Enter element: a
['a']
Enter element: b
['b']
Yes
In [10]:
def isPal(x):
    assert type(x) == list
    temp = x
    temp.reverse
    if temp == x:
        return True
    else:
        return False

def silly(n):
    result = []
    for i in range(n):
        elem = input('Enter element: ')
        result.append(elem)
        print(result)
    if isPal(result):
        print('Yes')
    else:
        print('No')
In [11]:
silly(2)
Enter element: a
['a']
Enter element: b
['a', 'b']
Yes
In [13]:
def isPal(x):
    assert type(x) == list
    temp = x
    temp.reverse
    print(temp, x)
    if temp == x:
        return True
    else:
        return False

def silly(n):
    result = []
    for i in range(n):
        elem = input('Enter element: ')
        result.append(elem)
    if isPal(result):
        print('Yes')
    else:
        print('No')
In [14]:
silly(2)
Enter element: a
Enter element: b
['a', 'b'] ['a', 'b']
Yes
In [16]:
def isPal(x):
    assert type(x) == list
    temp = x
    print('before reverse', temp, x)
    temp.reverse
    print('after reverse', temp, x)
    if temp == x:
        return True
    else:
        return False

def silly(n):
    result = []
    for i in range(n):
        elem = input('Enter element: ')
        result.append(elem)
    if isPal(result):
        print('Yes')
    else:
        print('No')
In [17]:
silly(2)
Enter element: a
Enter element: b
before reverse ['a', 'b'] ['a', 'b']
after reverse ['a', 'b'] ['a', 'b']
Yes
In [18]:
def isPal(x):
    assert type(x) == list
    temp = x
    print('before reverse', temp, x)
    temp.reverse()
    print('after reverse', temp, x)
    if temp == x:
        return True
    else:
        return False

def silly(n):
    result = []
    for i in range(n):
        elem = input('Enter element: ')
        result.append(elem)
    if isPal(result):
        print('Yes')
    else:
        print('No')
In [19]:
silly(2)
Enter element: a
Enter element: b
before reverse ['a', 'b'] ['a', 'b']
after reverse ['b', 'a'] ['b', 'a']
Yes
In [21]:
def isPal(x):
    assert type(x) == list
    temp = x[:]
    print(temp, x)
    temp.reverse()
    print(temp, x)
    if temp == x:
        return True
    else:
        return False

def silly(n):
    result = []
    for i in range(n):
        elem = input('Enter element: ')
        result.append(elem)
    if isPal(result):
        print('Yes')
    else:
        print('No')
In [22]:
silly(2)
Enter element: a
Enter element: b
['a', 'b'] ['a', 'b']
['b', 'a'] ['a', 'b']
No