4

I'm trying to do the second exercise in the Lists section of the book "How to Think Like a Computer Scientist". I basically have to match the given "doctest" with a program of my own that returns no error. I tried several ways, but despite the fact that the "Got" matches the "Expected" perfectly, it keeps giving me 1 failure.

I already saw one question here that asked "How can python 2 doctest fail and yet have no difference in values in the failure message?" I tried a few of the solutions given, like changing the test to "raw" by putting the r before it, but I don't think that the answer matches my case, because I checked several times after seeing this question and there isn´t a visible extra space where the problem seems to be.

This is the test I'm supposed to match:

"""
  >>> b_list[1:]
  ['Stills', 'Nash']
  >>> group = b_list + c_list
  >>> group[-1]
  'Young'
"""

And this is the program I wrote:

# Add your doctests here:
"""
    >>> b_list[1:]
    ['Stills', 'Nash'] 
    >>> group = b_list + c_list
    >>> group[-1]
    'Young'
"""
#Write your python code here:
b_list = ['a', 'Stills', 'Nash']
b_list[1:]
c_list = ['Young']
group = b_list + c_list
group[-1]

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

And this is the test result:

File ".\ch902.py", line 3, in __main__

Failed example:

    b_list[1:]

Expected:

    ['Stills', 'Nash']

Got:

    ['Stills', 'Nash']
**********************************
1 items had failures:
   1 of   3 in __main__
***Test Failed*** 1 failures.
NeoTrader
  • 119
  • 9
  • 1
    I copy pasted your sample code and it runs fine for me. I deliberately "broke" the doctest and it failed as expected as well. – idjaw Jul 30 '16 at 21:47
  • 1
    Possible duplicate of [python doctest: expected result is the same as the "got" result but the test failed](http://stackoverflow.com/questions/5856502/python-doctest-expected-result-is-the-same-as-the-got-result-but-the-test-fai) – Kevin J. Chase Jul 31 '16 at 04:04
  • 1
    Your copy of the doctest has an extra space at the end of `['Stills', 'Nash'] `. That looks the same as `['Stills', 'Nash']` when they're printed, but it's not the same string. (Suggestion: See if your editor has an option to automatically delete whitespace at the ends lines, or to highlight it, or at least a way to graphically show whitespace characters with symbols for spaces, tabs, and carriage returns.) – Kevin J. Chase Jul 31 '16 at 04:31
  • Thank you! That was it! On Notepad++, I went to Edit>Blank Operations>Trim Leading and Trailing Space, then I ran the doctest again and it gave me no error. – NeoTrader Jul 31 '16 at 16:37
  • There exists a thing such as non-printable whitespace characters. New lines and carriage returns are typically the culprit. – OneCricketeer Jul 31 '16 at 16:39
  • Nobody personally banned you, no moderator, no administrator. The ban is done algorithmically based on a metric of the quality of your questions. You might not like it, but having it in place greatly reduces the number of low-quality questions that are posted. – Hovercraft Full Of Eels Nov 15 '17 at 00:18

0 Answers0