"""
>>> ip_tuple = process("2\t1.001451000\t192.168.0.24\t\t10.0.0.5\t\t98\t84\t\t\t\t\t\t\t\t1")
"""
import doctest
def process(line):
list_lines = line.split('\t')
return (list_lines[2], int(list_lines[7]))
doctest.testmod()
When I run it, I would expect it to pass all tests, but the following output is produced:
e/lab4/Q9# python3 solution.py ********************************************************************** File "solution.py", line 2, in __main__ Failed example: ip_tuple = process("2 1.001451000 192.168.0.24 10.0.0.5 98 84 1") Exception raised: Traceback (most recent call last): File "/usr/lib/python3.6/doctest.py", line 1330, in __run compileflags, 1), test.globs) File "<doctest __main__[0]>", line 1, in <module> ip_tuple = process("2 1.001451000 192.168.0.24 10.0.0.5 98 84 1") File "solution.py", line 8, in process return (list_lines[2], int(list_lines[7])) IndexError: list index out of range ********************************************************************** 1 items had failures: 1 of 1 in __main__ ***Test Failed*** 1 failures.
Strangely when I get rid of the docstring and run the same code with the test, producing the following file:
def process(line):
list_lines = line.split('\t')
return (list_lines[2], int(list_lines[7]))
ip_tuple = process("2\t1.001451000\t192.168.0.24\t\t10.0.0.5\t\t98\t84\t\t\t\t\t\t\t\t1")
print(ip_tuple)
No error is produced and the following output is printed:
('192.168.0.24', 84)
Please explain why this is so.