-2

I have a problem with the following function in python (where swap is a function that I have previously created and that works fine):

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()

when I run this function it works fine but if I use doctest to see if there are mistakes it tells me:

TypeError: 'str' object is not an iterator

I don't know where this error comes from. Can anyone help me?

complete output of the 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
  • 2
    What argument are you passing into this function? – khelwood Nov 01 '16 at 15:39
  • Edit your question to show us how you run doctest, including the full output. – John Gordon Nov 01 '16 at 16:25
  • 1
    This function has no docstring, so I suspect the error is coming from somewhere else. – John Gordon Nov 01 '16 at 16:36
  • You can't call `next()` on a string. What are you trying to do? – John Gordon Nov 01 '16 at 16:53
  • i'm trying to find the indices of all the "B" in my string to then cut it in three peaces, (begining till first B, between first and last B and last B till the end). Then I can use my function swap (rearanges the letters) on the middle part and concatenate all together afterwards. – Manuel_MaStat Nov 01 '16 at 17:05
  • Show us the code that contains the `next()` call. – John Gordon Nov 01 '16 at 17:08
  • I've put my entire code now – Manuel_MaStat Nov 01 '16 at 17:15
  • 1
    [`next()`](https://docs.python.org/3/library/functions.html#next) is probably not what you expect it to be. Why do you think you can call it like that? And what does your other code even remotely have to do with that? Are you sure you didn’t mean to call e.g. `swap(…)` or something? Also, why are you running doctests, there are no docs in that source. – poke Nov 01 '16 at 17:21
  • I seriously doubt this is your whole code. It has no docstrings at all, and it contains no call to `next()`. – John Gordon Nov 01 '16 at 17:37
  • Now I added the docstrings but it was the only thing I hadn't put in my question. I never use next() . – Manuel_MaStat Nov 01 '16 at 18:55

1 Answers1

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

    # …

The function is called swap2 but within the doctests, you are using next which happens to be a built-in function that does something completely different. That’s why you are seeing that error.


At times like this, it’s really important to actually look at the error messages. It clearly told you what was called:

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

So if you don’t know where that was supposed to come from, then check out the error message. Doctest will tell you what it is executing: swap2[0], swap2[1], etc. tells you the function name the docstring that is being executed is by doctest and which test case it is (0 is the first, 1 the second etc.). It even gives you the line number (within the doctest case) where the error appeared, and of course the line that was causing the error. So use that information to go to the problematic code, and figure out what the problem is.

poke
  • 369,085
  • 72
  • 557
  • 602