Python for Kids Book: Project 6


In these posts I outline the contents of each project in my book Python For Kids For Dummies.  If you have questions or comments about the project listed in the title post them here. Any improvements will also be listed here.

What’s in Project 6

Project 6 introduces the concept of objects and lists to create a program that converts text into hacker speak (ie text with numbers substituted for letters). The project revisits the creation of a simple my_message variable then shows, using the dir builtin, that the variable has a variety of characteristics (that is, attributes) other than its value. It shows that one of the attributes, upper, is like a function and calls the functions of an object methods. It shows how to call a method or access an attribute through the dot notation.

In order to implement the hacker speak project, the program must perform a number of substitutions. To do that, it uses a list. The project discusses how to make a list,  how to add elements to a list, how to iterate through a list and how to test whether something is in a list.  It highlights a ‘gotcha’ with list methods – some of them modify the list in place without returning a value.

The code includes a logical error in the manner in which substitutions are made. A print statement is used to debug and identify the location of the error before it is fixed.

The project also gives a short introduction to IDLE’s debugger. There is a problem with IDLE’s debugger on Macs (there is no right click to set a breakpoint. Command-click works for some people, but not all), so if you’re running a Mac and command-click doesn’t work for you, you might have to skip this bit.

Improvements:
Page 150 does not use the print syntax from Python 3 like I said I would. It should be:

>>> for i in dir(my_message):
          print(i)
__add__
__class__
__contains__
[...]

Page 164 does not use the print syntax from Python 3 like I said I would. Should be:

>>> substitutions = [['a','4'], ['e','3'], ['l','1'], ['o','0'],
                     ['t','7']]
>>> for s in substitutions:
          print(s)

['a',  '4']
['e',  '3']
['l',  '1']
['o',  '0']
['t',  '7']

2 Responses to Python for Kids Book: Project 6

  1. pipedwho says:

    For setting breakpoints in IDLE on OS X, it’s Control-Click, not command-click.

  2. Terry Reedy says:

    I cannot yet run Python/IDLE on MAC. However, last fall (before 2.7.11, 3.4.4, 3.5.1) I pushed a patch written by someone else that should have resulted in right click properly bringing up a context menu (issue 24801). Set/clear breakpoint should be on that menu for all systems as the contents are defined by a system independent list. (I just rechecked.) If there is still a problem with context menus in a current release, please open an issue on the tracker. In any case, I recommend that people run the most recent version and bug-fix release of Python and IDLE that they can.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: