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']