Doctest returning failure, yet “Expected” and “Got

2019-05-07 03:38发布

问题:

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.