Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102914 > unrolled thread

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

Started byGeoff Munn <geoff.munn@gmail.com>
First post2016-02-14 05:39 -0800
Last post2016-02-15 13:17 -0800
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Syntax error (The Python Book) Linux User and Developer Bookazine Geoff Munn <geoff.munn@gmail.com> - 2016-02-14 05:39 -0800
    Re: Syntax error (The Python Book) Linux User and Developer Bookazine Peter Otten <__peter__@web.de> - 2016-02-14 14:53 +0100
    Re: Syntax error (The Python Book) Linux User and Developer Bookazine Chris Angelico <rosuav@gmail.com> - 2016-02-15 04:38 +1100
    Re: Syntax error (The Python Book) Linux User and Developer Bookazine Geoff Munn <geoff.munn@gmail.com> - 2016-02-15 06:56 -0800
      Re: Syntax error (The Python Book) Linux User and Developer Bookazine Joel Goldstick <joel.goldstick@gmail.com> - 2016-02-15 10:06 -0500
        Re: Syntax error (The Python Book) Linux User and Developer Bookazine Geoff Munn <geoff.munn@gmail.com> - 2016-02-15 07:33 -0800
        Re: Syntax error (The Python Book) Linux User and Developer Bookazine Larry Hudson <orgnut@yahoo.com> - 2016-02-15 13:17 -0800

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

FromGeoff Munn <geoff.munn@gmail.com>
Date2016-02-14 05:39 -0800
SubjectSyntax error (The Python Book) Linux User and Developer Bookazine
Message-ID<caa9384e-21e1-4ac4-a719-703cc0d1f44a@googlegroups.com>
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,



#!/usr/bin/env python2

'''
We are going to write a program that will ask for the user to input an arbitary
number of integers, store them in a collection, and then demonstrate how
the collection would be used with various control structures
'''

import sys  # Used for the sys.exit function

target_int=raw_input("How many integers?")

'''
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
'''

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

ints=list()  # list to store the integers

count = 0  # Track how many integers have been inputted

# Keep asking for a number until we have reached the required number
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")
        
    '''
    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
# 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

[toc] | [next] | [standalone]


#102917

FromPeter Otten <__peter__@web.de>
Date2016-02-14 14:53 +0100
Message-ID<mailman.109.1455458105.22075.python-list@python.org>
In reply to#102914
Geoff Munn wrote:

> 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. 

Often the actual syntax error in your code is in one of the lines preceding 
the one that Python is complaining about.

>     new_int=raw_input("Please enter integer {0}:".format(count +1)
>     isint = False

Hint: Do the parentheses match?

[toc] | [prev] | [next] | [standalone]


#102918

FromChris Angelico <rosuav@gmail.com>
Date2016-02-15 04:38 +1100
Message-ID<mailman.110.1455471501.22075.python-list@python.org>
In reply to#102914
On Mon, Feb 15, 2016 at 12:39 AM, Geoff Munn <geoff.munn@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

[toc] | [prev] | [next] | [standalone]


#102964

FromGeoff Munn <geoff.munn@gmail.com>
Date2016-02-15 06:56 -0800
Message-ID<f0f27bca-9462-4f16-9b1d-a1a16dd0f8f2@googlegroups.com>
In reply to#102914
On Sunday, 14 February 2016 13:39:52 UTC, Geoff Munn  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,
> 
> 
> 
> #!/usr/bin/env python2
> 
> '''
> We are going to write a program that will ask for the user to input an arbitary
> number of integers, store them in a collection, and then demonstrate how
> the collection would be used with various control structures
> '''
> 
> import sys  # Used for the sys.exit function
> 
> target_int=raw_input("How many integers?")
> 
> '''
> 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
> '''
> 
> try:
>     target_int=int(target_int)
> except ValueError:
>     sys.exit("You must enter an integer")
> 
> ints=list()  # list to store the integers
> 
> count = 0  # Track how many integers have been inputted
> 
> # Keep asking for a number until we have reached the required number
> 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")
>         
>     '''
>     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
> # 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

Thanks Peter and Chris, yes missed the parentheses by taking the error as being in line 31, DOH but a lesson learned. I have checked and checked the code I entered against the provided code and had to make some more changes to at least go through the first while loop but have given up on the rest of it. Given your comments do you think its worth persevering with this book or is there a better 'entry' into Python programming?

[toc] | [prev] | [next] | [standalone]


#102966

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2016-02-15 10:06 -0500
Message-ID<mailman.144.1455548803.22075.python-list@python.org>
In reply to#102964
On Mon, Feb 15, 2016 at 9:56 AM, Geoff Munn <geoff.munn@gmail.com> wrote:

> On Sunday, 14 February 2016 13:39:52 UTC, Geoff Munn  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,
> >
> >
> >
> > #!/usr/bin/env python2
> >
> > '''
> > We are going to write a program that will ask for the user to input an
> arbitary
> > number of integers, store them in a collection, and then demonstrate how
> > the collection would be used with various control structures
> > '''
> >
> > import sys  # Used for the sys.exit function
> >
> > target_int=raw_input("How many integers?")
> >
> > '''
> > 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
> > '''
> >
> > try:
> >     target_int=int(target_int)
> > except ValueError:
> >     sys.exit("You must enter an integer")
> >
> > ints=list()  # list to store the integers
> >
> > count = 0  # Track how many integers have been inputted
> >
> > # Keep asking for a number until we have reached the required number
> > 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")
> >
> >     '''
> >     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
> > # 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
>
> Thanks Peter and Chris, yes missed the parentheses by taking the error as
> being in line 31, DOH but a lesson learned. I have checked and checked the
> code I entered against the provided code and had to make some more changes
> to at least go through the first while loop but have given up on the rest
> of it. Given your comments do you think its worth persevering with this
> book or is there a better 'entry' into Python programming?
> --
> https://mail.python.org/mailman/listinfo/python-list
>


Learn Python the Hard Way is pretty good some people say.  Its online.
Also Diving into Python is online written by the now offline Mark Pilgrim.
-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays

[toc] | [prev] | [next] | [standalone]


#102968

FromGeoff Munn <geoff.munn@gmail.com>
Date2016-02-15 07:33 -0800
Message-ID<14f5a7c7-ff9b-4f9a-ba5b-5a624f253a7f@googlegroups.com>
In reply to#102966
On Monday, 15 February 2016 15:07:03 UTC, Joel Goldstick  wrote:
> On Mon, Feb 15, 2016 at 9:56 AM, Geoff Munn <geoff.munn@gmail.com> wrote:
> 
> > On Sunday, 14 February 2016 13:39:52 UTC, Geoff Munn  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,
> > >
> > >
> > >
> > > #!/usr/bin/env python2
> > >
> > > '''
> > > We are going to write a program that will ask for the user to input an
> > arbitary
> > > number of integers, store them in a collection, and then demonstrate how
> > > the collection would be used with various control structures
> > > '''
> > >
> > > import sys  # Used for the sys.exit function
> > >
> > > target_int=raw_input("How many integers?")
> > >
> > > '''
> > > 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
> > > '''
> > >
> > > try:
> > >     target_int=int(target_int)
> > > except ValueError:
> > >     sys.exit("You must enter an integer")
> > >
> > > ints=list()  # list to store the integers
> > >
> > > count = 0  # Track how many integers have been inputted
> > >
> > > # Keep asking for a number until we have reached the required number
> > > 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")
> > >
> > >     '''
> > >     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
> > > # 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
> >
> > Thanks Peter and Chris, yes missed the parentheses by taking the error as
> > being in line 31, DOH but a lesson learned. I have checked and checked the
> > code I entered against the provided code and had to make some more changes
> > to at least go through the first while loop but have given up on the rest
> > of it. Given your comments do you think its worth persevering with this
> > book or is there a better 'entry' into Python programming?
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> 
> 
> Learn Python the Hard Way is pretty good some people say.  Its online.
> Also Diving into Python is online written by the now offline Mark Pilgrim.
> -- 
> Joel Goldstick
> http://joelgoldstick.com/stats/birthdays

Thanks Joel

[toc] | [prev] | [next] | [standalone]


#102974

FromLarry Hudson <orgnut@yahoo.com>
Date2016-02-15 13:17 -0800
Message-ID<mbidndqVnZT53V_LnZ2dnUU7-TudnZ2d@giganews.com>
In reply to#102966
On 02/15/2016 07:06 AM, Joel Goldstick wrote:
[snip a lot...]
>
> Learn Python the Hard Way is pretty good some people say.  Its online.
> Also Diving into Python is online written by the now offline Mark Pilgrim.
>
I have a couple of "Hard Way" books and personally, I don't like his style of teaching.

Of course, take that as one person's opinion -- and as always, YMMV.   :-)

      -=- Larry -=-

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web