Multiplying Strings

10 Oct, 2017

Robin L writes (excerpted):

I am trying to teach myself how to code and thought this was a good place to begin. I am having trouble with the "multiplying strings" section. I don't know anyone else who codes so I am hoping that you are still available at this email. This is the part about printing a letter in the shell. For some reason when I run mine the "print()" keeps actually printing "()".

Can you please help me figure out what I am doing wrong?

There's a pretty major difference between printing with older versions of Python (Python2.7 and earlier) and newer versions (Python3 and later). In Python2, print is a statement, which means this works fine:

print "hi there"

If you try that in Python3, you'll get an error:

print "hi there"
  File "<stdin>", line 1
    print "hi there"
SyntaxError: Missing parentheses in call to 'print'

That's because print in Python3 is a function not a statement.

Why does that make a difference? In Python2, this code...

print() a print statement followed by an empty tuple. You're effectively telling Python to "print this empty tuple", and by quirk of the way the print statement works, you get (). The exact same code in Python3 is a function name (print) followed by an open bracket, no parameters, and a closing bracket. You're providing no parameters and the function prints nothing as a consequence. And that, basically, is the difference.

Cutting a long story short - all you're doing wrong is running an older version of Python. Check chapter one, and follow the instructions to install Python3, and the code will work as you expect.

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!!


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:

...with this...

    while True:
except TclError:

You can find more about error handling here:

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:

If we run this code right now we get:


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:
    for j in hugehairypants:
        print(j) 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