[Tutor] Do not understand why I am getting "EOFError: EOF when reading a line".

Walter Prins wprins at gmail.com
Sat Oct 8 21:04:58 EDT 2016


Hi

On 8 October 2016 at 05:51, boB Stepp <robertvstepp at gmail.com> wrote:
> I think I now understand why I am getting this EOF exception.
>
> On Fri, Oct 7, 2016 at 10:16 PM, boB Stepp <robertvstepp at gmail.com> wrote:
>> My current get_input() function:
>>
>> ======================================================
>> def get_input():
>>     '''Get string input from the user and convert it to an integer.  This
>>     integer is returned to the caller.
>>
>>     :num_sides:  Number of sides for the displayed grid.'''
>>
>>     while True:
>>         try:
>>             num_sides = int(input("Number of sides = "))
>>             return num_sides
>>         except ValueError:
>>             print("'Number of sides' must be a positive integer.  Please enter "
>>                     "an integer.")
>> =======================================================
>
> Since the try/except is within a while loop, the input tries to read
> more input after the ValueError occurs.  Of course, I don't have any
> more input strings stored in the StringIO buffer, so it sees this as
> an end of file condition.
>
> But now I do not see how to test get_input() to check for the
> ValueError.  Any suggestions?

Your test is written such that it checks/expect ValueError to be
thrown from get_input().  However, you're in fact catching/trapping
ValueError inside get_input() so it's never going to be seen by the
test.

You cannot have it both ways.  Either your intention is that
get_input() must raise ValueError as your test expects it (and
therefore get_input() shouldn't be [responsible for] catching,
printing and looping as it currently is), or it must not (and should
keep trying until it successfully reads an int).  If the latter is the
case your test needs to change to reflect this reality (by e.g.
perhaps altering what's in the StringIO() input for example, so that
there is a valid in in the input in addition to the invalid 'a', to
allow the loop to terminate.  Then the test should simply check that
get_input() successfully "ignores"/skips over the invalid input and
retrieves that latter integer from the test "input stream" [e.g. what
you put into the StringIO()] and not expect an exception to be
raised.)

Walter


More information about the Tutor mailing list