Reviewers wanted

If you’ve got a relevant blog and are interested in reviewing my book Python for Kids for Dummies, the publishers have some review copies that can be sent out. Please use the contact form on the About page to drop me a line and say you’re interested in reviewing the book. I’ll get your details and have a copy sent out.

Python for Kids Book: Project 10

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 10 (Math Trainer)

Project 10 is the last of the projects in the book. Project 10 is a math trainer – it tests you on your times tables and prints them out to allow you to revise.  This project is largely an application of some of the earlier work in the book but involves a little added complexity and some thought in solving some user interface issues.  For example, I use a formatting template to present the times tables. It’s easy to print out a long list of the times tables, but not very readable, so I show you how to use a nested loop to present 5 tables at a time across the screen. This uses the slice operator [:] as well as string’s join method

In order to ask random questions I introduce random.choice.  The trainer allows you to compete against yourself on score and on time. In order to time each training session I introduce the time method from the time module – time.time().

There are three additional projects that, as at 8 September 2015, I have completed and am now waiting to be loaded online.

Improvements:

@306: The time module gives you the number of seconds since the Epoch. The Epoch started on 1 January 1970 (for Unix operating systems). Why is it 1 January 1970? No reason. It’s just one of those historical accidents that infest everything to do with time calculations.

Python for Kids Book: Project 9

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 9 (Address Book)

While Project 6 introduced the concept of objects and showed how even a lowly string is actually an object, Project 9 is about how you can make your own objects using the class keyword.  Project 9 uses classes to implement a simple address book application.  I discuss the difference between a class and an instance of the class and the importance of having a reference variable (self) so that an instance can refer to itself. You learn about constructor functions and how to override a method (__repr__) to make print statements work properly on your custom method.

Since having an address book is not much use if you can’t save and update it, I also introduce the pickle module. It is used to store general Python objects (if they’re hashable!).  The address book is given save and load functions as well as a rudimentary user interface.

Improvements (1st printing):

On page 278 in the add_entry method, the second through fourth occurrences of the code

if first_name == "q":

Should be


if family_name == "q":

if email_address == "q":

and

if date_of_birth == "q":

respectively. This should be corrected in the second printing. The code samples have the correct code.

Python for Kids Book: Project 8

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 8 (Silly Sentences)

Project 8 is a short project which teaches you about (old style) formatting strings. Formatting strings allow you to create a sentence template into which you can substitute words.  Four words are chosen from four lists (at random) and substituted into a sentence. The random matchings make nonsense sentences. The substitutions can be streamlined by using a data type called the tuple, so that data type is introduced and discussed. You learn how to unpack tuples and how to use a tuple to receive multiple values from a function.  The project also relies on the random.choice method.

Python for Kids Book: Project 7

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 7

Project 7 (Cryptopy) introduces dictionaries as a means of encoding text using a Caesar cipher. Along the way, you are introduced to the string module and the characters in string.printable. There is some discussion about escape sequences and examples of \n and \t are given. Since you don’t want to encrypt escape sequences the slicing operator is introduced in order to take string.printable and slice off the control characters. This becomes the character set that will be encoded.

An encryption dictionary is created and each of the characters in a test message are encrypted then joined using the join method of the empty string.  I explain why this is better than adding one character after another to an existing string.  I create a matching decryption function and decryption dictionary and test the round trip (plaintext-> ciphertext -> plaintext).

The project introduces file operations by reading a message from a file then writing the encrypted (or decrypted) message to another file.  This is first done with the base file operations open and close, then the with keyword is introduced to make the housekeeping a little easier.

I demonstrate how to use your newly written encryption functions from the command line by importing the code from your own file – your own third party module!  In order for this to work seamlessly you are introduced to the __name__ attribute and the if __name__ == “__main__”: construction.

Improvements:

On page 203, the first line of code:

>>> file_object.close()

is not necessary  (because you already closed the file in the code on the previous page. It shouldn’t give you an error message though).  Ignore  it.

On page 209, there is a reference to code being in C:\Python27. This reference is only relevant if you are using Windows. On Linux and Mac things are more complicated – if the file is in the same directory that the shell is running from then you should be ok.

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

Python for Kids Book: Project 5

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 5

Project 5 introduces functions by revisiting the Guessing Game from Project 3 and recasting it using a function.  The project covers the def keyword, calling a function, the fact that a function must be defined before it can be called. The project also covers how to communicate with a function (both sending information to it by passing parameters and getting information from it, using the return keyword). In order to define a function, you need to give it a name, so the project sets out naming rules for functions. You should also be documenting your code, so the project introduces docstrings, how to create them, what to put in them and how to use them.

The project illustrates a logical problem in the code an explains what a local variable is. It introduces the concept of constants defined in the body of a program that can be accessed by code within a function.  A function which conducts the game round is put inside a while loop. The user interface is changed to allow the user to exit the loop.  This involves creating a quit function which first checks with the user to confirm that they want to quit, then using the break keyword to break out of the loop to quit, or the continue keyword if the user aborts the quit. The sys module is introduced in order to use sys.exit.

Improvements:

The name of the project is actually “A More Functional Guessing Game” – named as such since it will be using a function to make the guessing game work better, but some editor somewhere had a humor transplant and changed that title.

The callout on Figure 5-4 should read “The right place for an argument.”  They completely ruined that pun <sigh>

The code at the bottom of page 133 should read:

QUIT = -1
QUIT_TEXT = 'q'
QUIT_MESSAGE = 'Thank you for playing'

That is, an additional constant QUIT_MESSAGE = ‘Thank you for playing’ should be at the bottom of the page.

The line avg = str(total_guesses/float(total_rounds)) in step 4 on page 135 should be 4 lines down – being the first line in the else: block. Otherwise the logic does not work properly when you quit in the first round. The corrected code reads:

    # new if condition (and code block) to test against quit
    if this_round == QUIT:
        total_rounds = total_rounds - 1
        # removed line from here
        if total_rounds == 0:
            stats_message = 'You completed no rounds. '+\
                              'Please try again later.'
        else:
            avg = str(total_guesses/float(total_rounds)) # to here
            stats_message = 'You played ' + str(total_rounds) +\
                              ' rounds, with an average of '+\
                              str(avg)
        break

This same correction needs to be made to the “Complete Code” on page 138.

Follow

Get every new post delivered to your Inbox.

Join 85 other followers