功能错误:' str' object不是迭代器

时间:2016-11-01 15:33:28

标签: python function python-3.x doctest

我在python中遇到以下函数的问题(其中swap是我之前创建的函数,并且工作正常):

def swap (cards):
     """
     >>> swap('FBFFFBFFBF')
     'BFBBBFBBFB'
     >>> swap('BFFBFBFFFBFBBBFBBBBFF')
     'FBBFBFBBBFBFFFBFFFFBB'
     >>> swap('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
     'BBFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFB'
     """
    invert=""
    for i in cards:
        if i is "B":
            invert+="F"
        else:
            invert+="B"
    return (invert)

def swap2 (cards):    
    """
    >>> next('FBFFFBFFBF')
    'FFBBBFBBFF'
    >>> next('BFFBFBFFFBFBBBFBBBBFF')
    'FBBFBFBBBFBFFFBFFFFFF'
    >>> next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
    'FFFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFF'
    """
    indices=""
    for pos, i in enumerate(cards):
        if i =="B":
            indices+=str(pos)
    first= int(indices[0])
    last=  int(indices[-1])
    prefix= cards [:first]
    middle= cards [first:last+1]
    suffix= cards [last+1:]
    middle2=swap(middle)
    return (prefix+middle2+suffix)

def turns (cards):
    """
    >>> turns('FBFFFBFFBF')
    3
    >>> turns('BFFBFBFFFBFBBBFBBBBFF')
    6
    >>> turns('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
    14
    """
    turn=0
    while cards != 'F'*len(cards):
        cards=swap2(cards)
        turn+=1
    return (turn)

if __name__ == '__main__':
    import doctest
    doctest.testmod()

当我运行此功能时,它工作正常,但如果我使用doctest查看是否有错误,它会告诉我:

TypeError: 'str' object is not an iterator

我不知道这个错误来自哪里。 任何人都可以帮助我吗?

完成doctest的输出:

File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 25, in __main__.swap2
Failed example:
    next('FBFFFBFFBF')
Exception raised:
    Traceback (most recent call last):
      File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__.swap2[0]>", line 1, in <module>
        next('FBFFFBFFBF')
    TypeError: 'str' object is not an iterator
**********************************************************************
File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 27, in __main__.swap2
Failed example:
    next('BFFBFBFFFBFBBBFBBBBFF')
Exception raised:
    Traceback (most recent call last):
      File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__.swap2[1]>", line 1, in <module>
        next('BFFBFBFFFBFBBBFBBBBFF')
    TypeError: 'str' object is not an iterator
**********************************************************************
File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 29, in __main__.swap2
Failed example:
    next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
Exception raised:
    Traceback (most recent call last):
      File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__.swap2[2]>", line 1, in <module>
        next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
    TypeError: 'str' object is not an iterator

1 个答案:

答案 0 :(得分:0)

def swap2 (cards):    
    """
    >>> next('FBFFFBFFBF')
    'FFBBBFBBFF'
    >>> next('BFFBFBFFFBFBBBFBBBBFF')
    'FBBFBFBBBFBFFFBFFFFFF'
    >>> next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
    'FFFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFF'
    """

    # …

该函数名为swap2,但在doctests中,您正在使用next恰好是built-in function,它完全不同。这就是你看到错误的原因。

在这种情况下,实际查看错误消息非常重要。它清楚地告诉你所谓的:

File "<doctest __main__.swap2[0]>", line 1, in <module>
  next('FBFFFBFFBF')

因此,如果您不知道应该从哪里来,请查看错误消息。 Doctest会告诉你它正在执行什么:swap2[0]swap2[1]等告诉你正在执行的docstring的函数名是doctest,它是哪个测试用例(0是第一个,1第二个等等。它甚至会为您提供出现错误的行号(在doctest案例中),当然还有导致错误的行。因此,使用该信息来查找有问题的代码,并找出问题所在。