Syntax error (The Python Book) Linux User and Developer Bookazine

Chris Angelico rosuav at gmail.com
Sun Feb 14 12:38:13 EST 2016


On Mon, Feb 15, 2016 at 12:39 AM, Geoff Munn <geoff.munn at gmail.com> wrote:
> Hi,
>
> Noob at the Python thing so here goes,
>
> I have copied a program to demonstrate control structures in Python but get a syntax error at line 31, isint = False. I'm using Python 2.7.6 and Linux Mint based around ubuntu14.04.1. I have pasted all the code below,
>

Peter's already explained your actual problem, so I'll make a few
other comments about the code - starting with one about... comments.

> '''
> By now the variable target_int contains a string representation of
> whatever the user typed. We nee to try and convert that to an integer but
> be ready to deal with the error if it's not. Otherwise the program will crash
> '''

This isn't a comment. It's a triple-quoted string literal. Since a
string literal as an expression on its own is legal and insignificant,
this doesn't hurt much, but just be aware that these blocks aren't
comments. (BTW, is the typo "nee to try" (s/be "need") from the
original?)

> try:
>     target_int=int(target_int)
> except ValueError:
>     sys.exit("You must enter an integer")

What does "crash" mean? In the case of Python, failing to check for
this exception will result in a message printed to stderr and program
termination. Instead, the exception is caught... and a message is
printed to stderr and the program terminated. Is it really worth the
effort?

> while count < target_int:
>     new_int=raw_input("Please enter integer {0}:".format(count +1)
>     isint = False
>     try:
>         new_int=int(new_int)  # If the above succeeds then isint will
>         #be set to true: isint = True
>
>     except:
>         print("You must enter an integer")

A bare except clause! Bad idea. Never do this. In the *extremely* rare
cases when you actually do want to catch absolutely everything, you
can spell it "except BaseException:", but most of the time, you want
to catch one specific exception.

>     '''
>     Only carry on if we have an integer. If not we will loop again.
>     The == below is a comparision operator, a single = is an asignment operator
>     '''
>     if isnit==True:
>         ints.append(new_int)  # Adds the integer to the collection
>         count += 1  # Count is incremented by 1

Despite your comments, isint is never set to true - and isnit is
always going to be a NameError. Based on the number of typos here, I'm
wondering if we can actually depend on the code that IS having
trouble, which Peter mentioned as being a parenthesis count; maybe
these are all transcription errors?

> # The for loop
>     print ("Using a for loop")
>     for values in ints:
>         print (str(value))
> # The while loop
>     print ("Using a while loop")
>     total=len(ints)  # We already have the total from above but using len we can determine from the ints list.
>     count = 0
>     while count < total:
>         print (str(ints[count]))
>         count += 1

These two loops are inside your outer while loop - is that intentional?

I strongly recommend not using the name 'count' for two completely
different jobs in the same loop. Although the first one isn't actually
used, which kinda makes it a bit pointless.

This is not an example of Python best practice. It may be teaching you
some things, but don't imitate its style.

ChrisA


More information about the Python-list mailing list