Stick figure error

03 Jan, 2016

WangXin writes (excerpted):

I got a confusing error from the Python 3.4 (command line - 64 bit), it shows me this: "NameError: name 'collided_left is not defined'" , I have checked my code over and over again and I can't find the error. Could you help me to check my code when convenient?

These sort of errors are (luckily) fairly easy to figure out. Any time you see an error about something not being defined (and you're sure you've got the spelling correct) you can usually trace it back to a mistake in how you have indented your code.

Here's a relevant example from the book:

indentation example (small image)

In this code, you can see that "class Coords" and "def within_x" have the same indentation (they're both at the left margin). Now if we look at the code you've entered, with visible whitespace, you can hopefully see the difference:

incorrect indentation (small image)

Your within_x, within_y and collided_ functions are all indented to the same level as the __init__ function of the Coords class -- in short, Python looks at them and thinks: "those are indented the same as the init for the class, therefore they must be member functions of the same class".

So, if you fix the indentation, I think you'll find your code suddenly works as expected:

indentation corrected (small image)

Tkinter mainloop issue on Raspberry Pi

26 Aug, 2015

Martin writes:

First of all thank you for writing "Python for Kids". It's a great book and offers "instant gratification". My 8-year-old is already hacking his way through the pages on his brand new Raspberry Pi.

He (we) ran into a bit of trouble when we started playing with Tkinter.

Your initial examples don't ever call mainloop() or update(). Turns out, on the Raspi, in IDLE, those examples don't work as expected - nothing appears on screen. Until you run mainloop() that is. raspberry pi screenshot

If you run a pure python3 interpreter inside a shell window, your examples work. [They will also not work on Windows inside a Python 2.7.10 IDLE.]

Could you throw any light on why things work without calling mainloop() on some setups but not on others?

Turns out that trying to reproduce your problem - without having a RPi to hand - was a lot more difficult than I expected. Even after getting Raspbian going with QEMU, I couldn't get a menu to display so that I could run IDLE (solution: create an empty file, and from the file's context menu you can then access the Terminal). However, when I ran IDLE, it only worked in "no subprocess mode" (see the first chapter of the book). If I run without specifying -n, I get the following error:

qemu error (small image)

Only after going through the above, did I remember that there was an issue with IDLE & tkinter on Python 3.2 (when running without -n). And sure enough, when running with 3.2 on my PC, I get the exact same problem (it doesn't occur with later versions of Python).

Best thing you can do, is to run IDLE with the -n flag - the turtle/tkinter code you run will be more stable as a consequence (and there's no need to call mainloop).

(PS. as for Python 2.7.x, many of the code samples won't work - the book is only intended to be used with Python 3.x)

Python 3.5 beta issue on Windows

21 Aug, 2015

Tony writes:

I have installed Python 3.5.0b3, and when I tried to make the shortcut, it looked very different from the book's examples and I did not get the toolbar with "File", "Edit", "Options", etc. on the top. Please tell me how to correctly complete the installation process. Thank you, and I hope to hear back.

I'm not sure exactly what error you're getting, but I would suggest that while learning programming, you should probably use the latest release version of Python, rather than the beta. So at time of writing, that's version 3.4.3.

I do know of one problem with 3.5, where you need to move the DLL found in directory c:\Python35\DLLs\Microsoft.VC140.CRT, up one level (to directory c:\Python35\DLLs), in order to get tkinter working -- which I guess could be part of your problem (as detailed here:

So that's simply moving the file here:

microsoft.vc140.crt file - original location

to here:

microsoft.vc140.crt file - new location

NOTE: this is no longer required for the release version of 3.5 (it was only necessary for the beta versions)

Syntax error in

14 Aug, 2015

I have recently bought your book, Python For Kids and I think it's great and easy to understand except for one small thing.

It tells me to put this into the Shell:

spaces = ' ' * 25
print('%s 12 Butts wynd' % spaces)
print('%s Twinklebottom heath') % spaces)
print('Dear Sir')

I do what it says, I click run module, but I get this error:
error in editor window
I'm new to this, so I probably made some extremely stupid mistake, and i'm sorry if this is the wrong place to ask.

When I enter your code, I get the same error. But once I click OK, in the editor window I see the following:
error in editor window
That red highlight, on the third line down, shows you on where the error has occurred - and hopefully gives an idea about what might have gone wrong. In this case, Python isn't expecting a bracket at the end, because you've got one earlier in the line: print('%s Twinklebottom heath') % spaces). If you remove that bracket -- it should look like this print('%s Twinklebottom heath' % spaces) -- you'll find the program runs as expected.

Deprecated switch

12 Jul, 2015

Sandy S writes:

I just purchased 3 copies of "Python for Kids" so that my 2 sons and I can begin our quest! Anyway I hate to bug you with this but we just downloaded the latest version of the Python suite and the –n switch for IDLE startup has a message about "being deprecated since version 3.4". Is there another startup scheme that we should use instead? Also what do you recommend, in addition to the fine book, to enhance the learning process? My sons are middle-school age and very math/science savvy. I have a hardware engineering background but no programming to speak of.

The -n switch is deprecated, but not yet removed. So it still works fine with that switch for the moment. I did see some discussion about it on one of the Python mailing lists (forget which now) but I don’t think there was a conclusion about what happens in the future with something like IDLE and the use of tkinter (perhaps someone else reads this and lets me know - in which case, I'll post their message here). In any case, safe to use.

In terms of enhancing the learning process, I'd say once they've gone through the book (at least covered the basics of programming) picking a project is a good idea. Static learning is fine up to a point (even working through examples in the book), but what really cements the learning process is finding something you actually want to work on. It doesn't matter what sort of project, game or otherwise. As long as it's something your boys are interested in (also think about things like the raspberry pi and robotics and so on — since you've got a hardware background that'll be a useful crossover).

Hope that helps.

Problem with IF statements

26 Feb, 2015

Art S writes (excerpted):

First, let me say that I am having a great time learning Python from Python for Kids, even though I am just a kid at heart. I'm an old, old guy (I'll be 52 in April), but my students keep me young - I've been teaching high school mathematics for 25 years, and I am finally getting around to checking "Learn Python" off of my to-do list.

Here's the problem. All of my code has been running fine through IDLE until I got to the chapter on IF statements. When I typed into IDLE the first example from Chapter 5, page 54, nothing happened - the string 'You are too old!' (coincidence?) was not returned. All of the other examples involving IF's lead to the same result - nothing.


I thought I would give it one more try, just to be sure. When I did, I accidentally hit enter twice, and the desired output appeared. I feel good that it worked, but why should I have to hit enter twice?

So there's nothing you're actually doing wrong here. When you hit enter after typing the second line (the print part), Python is just waiting for the next statement in that block of code. Python uses white-space for the program structure, so the only way it knows you've actually finished that particular block of code, is when you hit enter again on the next line. So Python only runs the code once you've actually said you've finished... and the way you say you're finished, in this case, is to hit enter on an empty line.


Miquel C writes:

Do you know where to learn Python? I've been searching and I've only found Codecademy. I'd be very happy if you know where to go from Python For Kids. All kind of learning would be great.

I also downloaded an IRC bot, and I've translated it to my language. I've been looking the code and learning a bit.

Once you've mastered the basics of programming, my suggestion would be to find a small project to work on. Rather than looking at other people's code, or reading another, more advanced book, try creating your own version of an application you're interested in. It should be challenging, but not impossible. For example, when I was younger, I learned a new programming language by creating my own version of Tetris. You'll hit problems you don't know how to solve, so you'll end up reading the Python documentation, or googling to find the answers, or asking questions on sites like StackOverflow. But that's the one of the best ways to carry on learning (in my opinion).

Error opening IDLE on Mac OS X 10.6

10 Jan, 2015

Lucas writes (excerpted):

When I try to open IDLE as instructed on page 11 I get an error message.

Error dialog

Any suggestions?

It depends on which version of Python you're running, but if you've installed the latest (3.4.2), then instead of putting the following in Automator:

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

Try putting this instead:

/Library/Frameworks/Python.framework/Versions/3.4/bin/idle3.4 -n

The reason the text in the book doesn't work is a difference between Automator in versions of OS X prior to 10.7 (I believe 10.7, anyway) and later versions of the operating system.

Incrementing photo filenames

28 Dec, 2014

Emmeline writes:

I'm twelve and I recently got a raspberry Pi camera module. I really wanted to do stop animation with it but, when I used Tkinter to make a button it would take a picture when I pressed it and save it as I specified. The problem is if I wanted to use the button again it deleted the old image ex image.jpg and replaced it with a new image (image.jpg). Do you have any suggestions of how to change the code so it will name the photos numerically. Ex image1.jpg then image2.jpg. I'm doing all this in idle3. Thanks a lot for the book. "Python for kids" is great.

Glad you like the book.

There's a few ways you can change your code to have an incrementing filename. You could write a class, which uses a variable to store the current number. The class would have a function which increments the variable and returns the value. Something like this would work:

>>> class Counter:
...     def __init__(self):
...         self.counter = 0
...     def next(self):
...         self.counter += 1
...         return self.counter

You could use the class by creating an object like this:

>>> c = Counter()

You could then create a function to return a unique image name each time it's called:

c = Counter()

def get_image_name():
    return 'image%s.jpg' %

You could also just adapt the code above to create a class which returns unique image names:

>>> class ImageNames:
...     def __init__(self):
...         self.counter = 0
...     def next_image_name(self):
...         self.counter += 1
...         return 'image%s.jpg' % self.counter
>>> im = ImageNames()
>>> im.next_image_name()
>>> im.next_image_name()

By the way, you can find more about embedding values in strings in Chapter 3, and classes and objects in Chapter in Chapter 8.

Hope that helps.

Well done Amazon - truly awful UX

22 Oct, 2014

How to ensure that your customers never, ever... ever want to sign up for Prime... ever:

Step 1.
Prompt user to sign up for Amazon Prime while processing their order.

Step 2.
Customer clicks "No Thanks" (because customer was previously signed up for Prime and decided they didn't want to pay for it - so of course you should ask again and again... and again)

Step 3.
Display final checkout page to customer and automatically select the paid shipping option (okay fine, that's fairly normal)

Step 4.
Display popup window to customer asking them to sign up for Amazon Prime again (did I mention again?), only this time position the Sign up link in the exact same position that that the radio button for "Free Super Saver Delivery" is displayed (because you know that's the button they're probably going to select). Better yet, you should time the display of this popup so that by the time they've moved the mouse cursor into place, customer is clicking on said radio button at the very moment the popup appears.

Step 5.
By no means should you ask for confirmation before signing the customer up, when they do mistakenly click.

Frankly, don't know whether it was just exceptionally poor, or whether it was nefarious UX design to get people on Prime, but it's a really, really good way to annoy your customers. Or at least this customer.

Multiple statements found

08 Sep, 2014

Arijjan V writes (excerpted):

When I enter code into the compiler I continue to get multiple statement error messages. Even if I copy the code from the book.

I use Idle 34 on windows professional 7. This is what I typed into the Idle Shell.

found_coins = 20 magic_coins = 70 stolen_coins = 3 coins = found_coins for week in range(1, 53):     coins = coins + magic_coins - stolen_coins     print('Week %s = %s' % (week, coins)) **Click Enter** SyntaxError: multiple statements found while compiling a single statement

I searched online but it's not an indent error. I'd appreciate any help.

To be honest, I'm not quite sure how you managed to get that error when typing the code into IDLE - the only way I can get it to happen, is if I copy-and-paste directly into the Shell:

Multiple statements showing error

The reason being is that you can only copy-and-paste line-by-line into the Shell window (a quirk of the way it works). What you should actually be entering looks like this:

Multiple statements without error

If you want to paste in a large chunk of code, then click File, then New File, then paste the code, and save the file before trying to run it:

Multiple statements in new file

If you can reproduce the problem when typing in the code, send a screenshot (take it after entering a few lines) -- I'd be interested to see if it's obvious from the screenshot what the cause is...