Stickman Refusing To Move

16 Jul, 2017

Haran R writes:

I tried out the code for the two games and have successfully run the first game (bounce). The second game however did not run like how i wished it to. I followed the book’s instructions to the letter, used the GIMP software to create the images, saved it in a directory and saved the code in the same directory. The graphics came out fine but unfortunately I have not been able to get the stickman to move. I have followed the coding in the book to the letter and have checked my code against the coding in the book multiple times and i can see no mistakes.

The problem is with the indentation in your Stick Man class. If you look at the indentation of your code...

indentation 1

and then compare with mine...

indentation 2

...hopefully you'll see where you've gone wrong (if it's not immediately obvious, take a look at the vertical line coming down from the animate function in each of the code samples, and you'll see where the indentation isn't right). When I fixed the indentation on your code, it worked perfectly -- so you were almost there.

Hope that helps.


Tcl Error

11 May, 2017

丁明 writes (edited for spelling):

In your book <<python for kids>> chapter 13, when I run the program, the python shell has a error I can't understand. my python version 3.6.0. Thank you for your reply!!

error.png

I'm not sure if the Chinese edition has captured the text on page 199 of the English version of the book:

You may see error messages written to the shell when you close the game window. This is because when you close the window, the code is breaking out of the while loop, and Python is complaining about it.

The is exactly the error you are seeing when you run that code and then close the window. You are stopping (interrupting) Python while it is running the code, and an error message is displayed as a consequence.

Basically, you can ignore the message, but if you want to know how to get rid of it for good, one option is to replace the loop at the bottom:

while True:
    ball.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

...with this...

try:
    while True:
        ball.draw()
        tk.update_idletasks()
        tk.update()
        time.sleep(0.01)
except TclError:
    pass

You can find more about error handling here: https://wiki.python.org/moin/HandlingExceptions


Windows 10

08 Apr, 2017

I've come across some uncorroborated reports that some of the instructions, or examples, in Python for Kids don't work with Windows 10. Uncorroborated because it's unfortunately nothing more than "it don't work". Not spectacularly useful, so I've finally gotten around to installing Windows 10 in VirtualBox to test this out.

Instructions for Python installation remain pretty much the same, except that the latest version of Python is now 3.6, whereas on the last printing of PfK, it was 3.5. The results of installation aren't too dissimilar to Windows 7 either:

windows-10 start menu (small image)

I cherry-picked a few of the code samples at the beginning of the book to test, but wasn't expecting any of those to be a problem (they weren't), and then went through the more complicated code (turtle, tkinter) in the rest. Everything works as expected, except there is a little bit of graphical weirdness in the Bounce game (chapters 13, 14) where the ball seems to deform as it moves. I'm pretty certain this is an artifact of using Windows inside VirtualBox (I've never found animated graphics to work particularly well when you're in an OS inside an OS) - it's certainly not a problem in Linux or on a Mac. But interested to hear if anyone else has experienced the same issue on Windows:

Deformed ball


That's pants!

05 Apr, 2017

Lee writes:

I'm not fully understanding the HugeHairyPants program on pages 72-73 of Python For Kids. I initially thought that the loop would produce "huge huge hairy hairy pants pants," reasoning that the program would assign the first value to i, then the identical value to j, and continue the loop. So why is the variable j assigned all of the values? My thought was that the program commands i to be the first variable ('huge'), then stops there because the variable j is introduced next. Since there is no other variables after j, it then gets assigned every variable. Is this correct? (It doesn't sound right to me, though.)

So let's break the code down a bit more then. Here's the list...

hugehairypants = ['huge', 'hairy', 'pants']

...and then the first two lines of the loop code:

for i in hugehairypants:
    print(i)

If we run this code right now we get:

huge
hairy
pants

Each time the value in i is (effectively) replaced with the next value in the list (it's actually not quite as simple as that, but it can be easier to think of it that way when you start programming).

It's important to remember that every time we loop here, we're executing all the statements in the block of code - that's the indented part. And that's why if we look at the original example...

for i in hugehairypants:
    print(i)
    for j in hugehairypants:
        print(j)

...it first executes print(i), then it executes the second for-loop - that's looping over the hugehairypants list again, calling print(j) three times, then back to print(i) again, and so on. If we just look at the print statements Python is executing in these two loops, this is what it looks like:

print(i) # first for-loop - the value in i is now 'huge'
print(j) # second for-loop - the value in j is now 'huge'
print(j) # second for-loop - the value in j is now 'hairy'
print(j) # second for-loop - the value in j is now 'pants'
print(i) # first for-loop - the value in i is now 'hairy'
print(j) # second for-loop - the value in j is now 'huge'
print(j) # second for-loop - the value in j is now 'hairy'
print(j) # second for-loop - the value in j is now 'pants'
print(i) # first for-loop - the value in i is now 'pants'
print(j) # second for-loop - the value in j is now 'huge'
print(j) # second for-loop - the value in j is now 'hairy'
print(j) # second for-loop - the value in j is now 'pants'

If you need something more visual, I suggest drawing two rectangles on a piece of paper with 3 cells. Write the 3 values from the list in the cells (obviously it's not really two lists, but easier this way). Get two small coins; the first coin marks the value in i, the second coin marks the value in j. Work through the code as though you were running it yourself - move each coin to show how the value changes as we loop:

Visual guide


Epic Amazon Fail

22 Mar, 2017

Frankly this is bizarre, but if you're looking for Python for Kids, I suggest you don't buy on Amazon:

Expensive!! (small image)

Much as I'd like to be getting a cut of that price, I somehow doubt I'm getting anything -- and you're possibly getting a poor quality knock off, going by this:

https://twitter.com/billpollock/status/844030960333152256

(Some more discussion on Hacker News here: https://news.ycombinator.com/item?id=13924546)

Barnes and Noble looks to be a safer option - and you can still buy directly from No Starch Press of course.

Further update: Inc.com "Amazon's Tepid Response to Counterfeiters Frustrates Sellers"