Python on Chromebook

01 Mar, 2016

Cynthia B writes:

I purchased your book Python for Kids so that my 11 year old daughter and I could start to learn code together. We are trying to install Python on her (lenovo 100S) chromebook to no avail.

We followed several prompts and purchased a python shell which has been installed but can not figure out where to go from here, or if this will even be possible. Any advice?

As far as I'm aware, the Python shell extensions available in the Chrome OS store aren't Python 3 compatible - so at least from a version perspective, that's a non-starter. Even if the version was correct, I suspect your mileage would probably vary with some of the later code examples in the book, using that sort of environment (not a fully fledged installation... again AFAIA).

I don't have a Chromebook to try it out myself, but from a bit of searching, it looks like you can dual-boot Linux, and after that should be able to install anything you like (http://stackoverflow.com/a/15261253/2685350 - although Samsung-specific, I would hope the basic idea still applies).

I've found some instructions on doing so at the following sites:
http://www.howtogeek.com/162120/how-to-install-ubuntu-linux-on-your-chromebook-with-crouton/
http://lifehacker.com/how-to-install-linux-on-a-chromebook-and-unlock-its-ful-509039343
http://www.linux.com/learn/tutorials/764181-how-to-install-linux-on-an-acer-c720-chromebook


Pendrive

21 Feb, 2016

Kishore writes:

I bought this great book "python for kids". I want to give it as a gift to kid in India. She doesn't have internet, but has a computer. What do I need to download and give in a pendrive, so that she can finish the book without an internet connection.

Apart from the book itself, there's two extras to download: 1 - the solutions to all the programming challenges at the end of each chapter, and 2 - the code samples in the book (in case she has difficulty getting something to work).

The solutions are available from the publishers website here: http://nostarch.com/pythonforkids. A zip file of all the code can be found here: /python-for-kids/code.zip.

Depending on when you purchased the book, you might also want to take a copy of the eratta. If it's a fairly recent reprint, there's no need. If the printing is before April 2015, then you should probably send that to her as well.

You didn't mention whether she already has Python installed, so if not, I suggest reading the first chapter yourself, so that you can include the right installation files for her OS.


A parameter called evt

26 Jan, 2016

Dudley B writes:

I'm a Business Analyst trying to learn how to code. Luckily there's Python, a suitable language for absolute beginners like me. Thank God also for your book, Python for Kids. It's a great book which explains Python fundamentals in a fun and easy way. In Chapter 14 of the book, page 208, there is a parameter called "evt":

def turn_left(self, evt):
    self.x = -2
def turn_right(self, evt):
    self.x = 2

I can't figure out what this parameter is for, and why it's there since there is no value passed on to it. Please help me understand why "evt" is needed for those functions.

The evt parameter isn't brilliantly explained in that chapter, now that I look back at it. If you go back to the section titled "Making an object React to something" in Chapter 12, there's a description there of event bindings. The function described takes a parameter called event. After you've "told" tkinter about the function (that's the binding part), when an event occurs (such as the mouse being moved, or keyboard being pressed) it calls that function with an object containing more information (such as what sort of event it was).

The parameter evt on page 208, is simply a shortened name for the parameter event mentioned in the earlier chapter. In fact, the name isn't important at all -- you could even call it bob, or aardvark, or anything else you like.

By the way, another way of thinking about the tkinter event binding is that it's like a contract between two people. Let's say between a surfer, and an old man living on a cliff above the sea. The surfer says to the old man, "send me a message and let me know how high the surf is, whenever it changes". The function is that contract, and the message about the surf is the parameter value.

Hopefully that helps.


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)