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

D

Dave Peterson

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
 
B

Benjamin Kaplan

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

Robert Kern

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
 
A

Andrej Mitrovic

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

David Malcolm

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-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.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
 
A

Andrej Mitrovic

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-28)] 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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top