[Tutor] Request for help with code

Steven D'Aprano steve at pearwood.info
Tue Nov 6 17:48:05 EST 2018


On Tue, Nov 06, 2018 at 03:50:42PM -0600, Joseph Gulizia wrote:
> I'm using the bookazine  "The Python Book" First Edition on pages 13-14 it
> gives the code (listed further below).
> 
> It asks for user to state a given number of integers (for example 4)...then
> user enters integers.  It doesn't stop seeking input after the number
> requested thereby creating an infinite loop.

The first and most important rule for asking for help with code is to 
always post plain, unformatted text, not "Rich Text" (formatted text, 
styled text). Don't add colours, highlighting, bold, italic, fancy 
fonts, dancing paperclips or anything else, because when you do, it 
destroys the necessary indentation of the code and makes it impossible 
to run or debug. For example, your code shows up like this:


# Begin the error check
try:
target_int=int(target_int)
except ValueError:
sys.exit("You must enter an interger")


In that case, we could(?) guess the correct indentation, but (1) 
sometimes we can't, and (2) we shouldn't have to.

The *second* most important rule for posting code is to *keep it 
simple*. We're volunteers, not slaves, and you should always ask first 
before posting masses of code for us to investigate. Anything more than, 
oh, say, thirty lines (including comments) counts as masses.

Please try reading this:

http://sscce.org/

it is written for Java programmers but the advice applies as well here. 
Before posting your entire code, take the time to simplify it to a 
*mimimal example* which demonstrates the same problem. Half the time 
this will allow you to solve the problem yourself, and the other half of 
the time, it makes it easier on us slaves! I mean volunteers.

At a *guess*, and this is purely a guess because the lack of indentation 
makes it impossible to understand the structure of your code, I think 
the infinite loop is probably intentional. If you guess wrongly, it asks 
you to guess again, *forever*, until you guess correctly.

But as I said, I can't really tell.



-- 
Steve


More information about the Tutor mailing list