Trouble with Automator

27 Jul, 2014

Jean-Jacques B writes:

I am trying to set up my iMac for the book, having trouble with the automator script:

open -a "Applications/Python 3.2/" --args -n

Run Shell Script failed open: unrecognized option '--args'

I can get python to launch the other ways, but then turtle doesn't work.

This had me stumped for a moment (couldn't find any similar errors when I googled).

It actually looks like a poorly handled error in the Automator. If you copy+paste what you entered into the Terminal, you get an entirely different error message:

$ open -a "Applications/Python 3.2/" --args -n  
Unable to find application named 'Applications/Python 3.2/'

Which hopefully helps you figure out where you went wrong... try adding a / (slash) in front of the path to IDLE. eg.:

open -a "/Applications/Python 3.2/" --args -n

Python 3.4 Shell

15 Jun, 2014

Kirk K writes:

On page 12 it says File>New Window but I cannot find it on our Windows version 3.4.1. Please advise.

It looks like, as of Python 3.4, they've changed the menu, so File->New Window has become File->New File (which makes the Shell consistent with other text editors, at least). It still has the same behaviour though - opening a new window for typing in your Python code.

Restart when running tkinter

13 Jun, 2014

Craig S writes:

My son is having a problem running the random rectangle program on page 174 of Python for Kids. He is using version 2.7.7, so the Tkinter module must be capitalized (just so you don't suggest to capitalize it). Every time he tries to run the module, it comes up with a new window saying, "RESTART". He tried to copy and paste your code, but came up with the same results. This is the code he's written.
from Tkinter import *
import random
tk = Tk()
canvas = Canvas(tk, width=400,height=400)
def random_rectangle(width, height):
random_rectangle(400, 400)

This is what comes up when he runs it.
Python 2.7.7 (default, Jun 1 2014, 14:17:13) on win32
Type "copyright", "credits" or "license()" for more information.
>>> =============== RESTART ===============

Please share some advice. Thanks.

A couple of thoughts:

  1. He's missing the pack function after the 4th line of that code (canvas.pack())
  2. You're probably not running Idle in "No Subprocess" mode (see Chapter 1 of the book for setup instructions). If that doesn't work for you, for some reason, then best thing to do is use a text editor, save the code and then run it from the command prompt by typing python (see for instructions on running the command prompt if you're not aware how to do that)
  3. You may hit other weird issues if you're not using Python3, since all the code in the book was written and tested with 3.x. Much of it is probably fine, but no guarantees there...

Which format?

12 Jun, 2014

Koki Y writes:

I have a question. You use % for format instead .format(). In the python tutorial, it is written that % format is an older version of format. What's the point of using % format?

The book uses the % operator (also known as the interpolation operator) basically through omission more than anything else. The first electronic version was written for Python2 (back in 2006/2007), then updated to be compatible with both 2 and 3. While it was almost totally rewritten, to be published as "Python for Kids", some of the existing text was repurposed -- however, the string formatting section wasn't specifically updated for Python 3. I suspect that's somehing we should probably fix when/if it's reprinted again.

By all means, use the format function as detailed in the Python documentation.

Questions and remarks on Python for Kids

19 May, 2014

Marcin S writes:

I am currently testing your book on my girlfriend who is not a kid anymore but a person that has not written any code in her entire life. To assist her better I have read the whole book. I think I spent my money well when I bought it.

Still, I have some questions and remarks.

Did you consider to emphasize more we are using Python 3 in the entire book? I know that the reader is instructed to download that version, but if someone uses Python 2 for some reason (e.g. computer science classroom with outdated software), one will get different results. In the context of your book I have found two important cases: 1) division always produces a float in Python3 because we no longer have a strict difference between integers and floats, 2) input() works like raw_input() from Python2, while input() in Python2 made implicit eval(). [1]

The errata mentions input() behaves differently than readline() depending on the context -- console or shell. I am unable to replicate it. BTW, the parameter to read()/readline() specifies the maximum number of characters to read, not the exact number. And there is one more thing -- if one reads less characters than available on stdin, remaining characters will be read on the next call to read()/readline(). [2]

Are strings enclosed in triple double-quotes omitted as reserved for documentation? I think only triple single-quotes are mentioned in the book. [3]

Why did you choose to name classes with plural names (class Giraffes) instead of singular (class Giraffe)? [4]

The "is" keyword is mentioned in the keywords section as a complicated concept and is omitted in the examples. I have one remark on that: PEP8 suggests using "is" when checking if something is None (not with "=="). [5]

I have seen range() with 3 arguments mentioned later in the book. Did you consider mentioning slices ([a:b:c]) as well? [6]

Did you consider mentioning "for" and "while" with the optional "else" block? [6]

The book mentions random.choice() but the bounce game uses shuffle() and picking by index instead. [7]

Mr. Stick Man checks for collisions using comparisons glued with "and". Did you consider constructs like "x1 < x < x2" instead of "x1 < x and x < x2"? [8]

Why did you choose a Tk construct based on sequence: update_idletasks(), update(), sleep() instead of mainloop() with rescheduling with Tk.after()? There would not be any active wait then and it would be event-driven. Is not the update_idletasks() included in update()? [9]

I've annotated your comments so I can address each in turn, but the answer to many of your points is somewhat similar:

[1] No, at the time of writing, I didn't consider more emphasis was needed, other than what is detailed in the first chapter. But as time has gone on, the differences between 2 and 3 account for a significantly large percentage of the questions I get about the book, so perhaps something additional makes sense.
[2] The problem is reproducible in Python3.2 (and older versions). In 3.3 onwards, the max characters parameter works as expected.
[3] No, the triple double-quotes were omitted essentially for simplification reasons. To make it less confusing for younger readers.
[4] I chose plural class names mainly to try to indicate class as a kind of grouping - trying to distinguish between that and singular object instances. Yes, not particularly standard, but I had a (possibly vain) hope it would help to highlight the difference between class and object.
[5] "is" was omitted again for simplification reasons -- to avoid a complicated description of how object identity works.
[6] Again, slices and else blocks in for/while loops were omitted for simplification.
[7] You'll find a description of the shuffle function on page 136.
[8] Another one omitted to simplify things. Combining conditions with and and or is, I believe, more straightforward for kids to understand (even if x < y < z is a bit cleaner)
[9] This one came up in the review from one of the technical proof readers. mainloop is definitely the better (read as correct) approach, but I wanted to stick with a loop and update/update_idletasks because it made it obvious, to the reader, what is happening. Each loop tells one of the objects to do something (move and draw), and then triggers Tk to redraw the screen (the reason for using both update and update_idletasks is that when writing the book -- with an earlier version of Python3 -- I saw some animation jittering when using only the one call. Using both seemed to resolve that issue. Perhaps I should've checked why at the time, and maybe it's no longer an problem with Python3.4)

Hope that helps clarify some of your questions...