Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Awful book warning: How to think like a (Python) programmer - non-working examples

Reply
Thread Tools

Awful book warning: How to think like a (Python) programmer - non-working examples

 
 
Dave Peterson
Guest
Posts: n/a
 
      02-08-2010
Page 7: Very first example doesn't compile: syntax error
Pate 11: 2nd example: syntax error
Page 12, printing digits: syntax error
Page 13, printing a number: syntax error
page 14, statements: syntax error





 
Reply With Quote
 
 
 
 
Benjamin Kaplan
Guest
Posts: n/a
 
      02-08-2010
On Mon, Feb 8, 2010 at 3:36 PM, Dave Peterson <(E-Mail Removed)> wrote:
> Page 7: Very first example doesn't compile: syntax error
> Pate 11: 2nd example: syntax error
> Page 12, printing digits: syntax error
> Page 13, printing a number: syntax error
> page 14, statements: syntax error
>


Let me guess, you're using Python 3.1. That book was written for
Python 2.x and there were several backwards-incompatible changes. For
instance, print was changed from a statement to a function. Which is
why the "Hello, World" doesn't work any more. If you want to use the
older books, use Python 2.6 instead.

>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
 
Robert Kern
Guest
Posts: n/a
 
      02-08-2010
On 2010-02-08 14:36 PM, Dave Peterson wrote:
> Page 7: Very first example doesn't compile: syntax error
> Pate 11: 2nd example: syntax error
> Page 12, printing digits: syntax error
> Page 13, printing a number: syntax error
> page 14, statements: syntax error


This book was written for the 2.x versions of Python. Are you using Python 3.1?
Python changed some of its syntax for version 3.0, notably

print "Hello, world!"

becomes

print("Hello, world!")

This accounts for all of the SyntaxErrors that you are seeing. The examples
aren't broken for the version of Python it is teaching.

You may want to try _Dive Into Python 3_ to learn about Python 3 in particular:

http://diveintopython3.org/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

 
Reply With Quote
 
Andrej Mitrovic
Guest
Posts: n/a
 
      02-08-2010
The book covers Python 2.x syntax.

You might have downloaded Python 3.1, which has different syntax then
Python 2.x. From what I can tell, the first example on page 7 is ">>>
print 1 + 1".

Try issuing this command:
print(1 + 1)

If everything goes well, and you get '2' as the answer, then you're
probably using Python 3.x. You will have to download the Python 2.x
binaries from the Python website, install Python 2.x, and try the
example from the book again.
 
Reply With Quote
 
David Malcolm
Guest
Posts: n/a
 
      02-08-2010
On Mon, 2010-02-08 at 12:53 -0800, Andrej Mitrovic wrote:
> The book covers Python 2.x syntax.
>
> You might have downloaded Python 3.1, which has different syntax then
> Python 2.x. From what I can tell, the first example on page 7 is ">>>
> print 1 + 1".
>
> Try issuing this command:
> print(1 + 1)
>
> If everything goes well, and you get '2' as the answer, then you're
> probably using Python 3.x. You will have to download the Python 2.x
> binaries from the Python website, install Python 2.x, and try the
> example from the book again.


Sorry to nitpick; the main thrust of the above sounds correct, in that:
print 1 + 1
works in Python 2 but fails in Python 3, but, a minor correction, note
that:
print(1+1)
does work in Python 2 as well as in Python 3; the parentheses are
treated (in the former) as denoting grouping of a subexpression, rather
than function invocation (in the latter):

Python 2.6.2 (r262:71600, Jan 25 2010, 13:22:47)
[GCC 4.4.2 20100121 (Red Hat 4.4.2-2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print(1+1)

2

This can be useful if you're trying to write short fragments of code
that work with both.

Look at the startup message, or run this command, which should work on
both python2 and python3:
import sys; print(sys.version)

Hope this is helpful
Dave

 
Reply With Quote
 
Andrej Mitrovic
Guest
Posts: n/a
 
      02-08-2010
On Feb 8, 10:14*pm, David Malcolm <(E-Mail Removed)> wrote:
> On Mon, 2010-02-08 at 12:53 -0800, Andrej Mitrovic wrote:
> > The book covers Python 2.x syntax.

>
> > You might have downloaded Python 3.1, which has different syntax then
> > Python 2.x. From what I can tell, the first example on page 7 is ">>>
> > print 1 + 1".

>
> > Try issuing this command:
> > print(1 + 1)

>
> > If everything goes well, and you get '2' as the answer, then you're
> > probably using Python 3.x. You will have to download the Python 2.x
> > binaries from the Python website, install Python 2.x, and try the
> > example from the book again.

>
> Sorry to nitpick; the main thrust of the above sounds correct, in that:
> * * print 1 + 1
> works in Python 2 but fails in Python 3, but, a minor correction, note
> that:
> * * print(1+1)
> does work in Python 2 as well as in Python 3; the parentheses are
> treated (in the former) as denoting grouping of a subexpression, rather
> than function invocation (in the latter):
>
> Python 2.6.2 (r262:71600, Jan 25 2010, 13:22:47)
> [GCC 4.4.2 20100121 (Red Hat 4.4.2-2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> print(1+1)
>
> 2
>
> This can be useful if you're trying to write short fragments of code
> that work with both.
>
> Look at the startup message, or run this command, which should work on
> both python2 and python3:
> * import sys; print(sys.version)
>
> Hope this is helpful
> Dave


Oops, you're right. I'm used to Python 3 syntax so I'm only aware of
some basic differences.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
A lens so awful the image looks like it came from a mid-levelsuperzoom P&S RichA Digital Photography 11 06-17-2010 07:17 AM
site layout is awful in FF/Opera, but not in IE (using CSS) Dano HTML 13 04-01-2005 02:43 PM
Help need with Pinnacle InstantDisc which is an awful program Stickems. Computer Support 2 03-10-2005 10:26 PM
My Site is Awful :( DJS2C HTML 9 12-04-2004 12:46 AM



Advertisments