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.

Python for Kids Book: Project 4

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 4

Project 4 introduces the integrated development environment, IDLE, that comes prepackaged with Python. In Project 4 you learn your way around the IDLE Shell and Editor Windows. I demonstrate syntax highlighting, tab completion and editor history (making your coding life easier), how to save a file, open a file and run a file from the Editor window . You also learn how to automatically indent and de-indent your code. This makes working with code blocks much easier!

The coding concepts in this project relate to adding comments to your files – how and where to do it and some tips on adding comments so they are useful in the future. I also show you how to use comments in debugging to skip over “comment out” sections of code.

Python for Kids Book: Project 3

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 3

Project 3 explores a guessing game from the command line. In order to get a guessing game up and running you need to know how to receive input from the user. This project introduces the raw_input() builtin to accommodate this.

In order to tell whether a guess is correct, the computer must compare the guess to an answer – using the == operator. Further, since raw_input returns a string, I explain that strings and numbers are different and introduce the int() builtin in order to be able to compare the input with a number.

To give a player feedback, you need to determine whether the guess was higher or lower than the desired number. The operators > and < are introduced to address this. The if conditional along with the variants elif and else are introduced to be able to structure the feedback given to a player.

To choose a number at random you learn about import, the random module and random.randint.

The while structure introduced in Project 2 is used to allow the player to make repeated guesses of the answer. This is coupled with the break keyword to exit the loop one the correct number is guessed.

In the course of introducing the ==, > and < operators, I also give an overview of more common operators used in Python (at Table 3-1).  After introducing the operators I discuss why division in Python is a special case, how to recognize problems with division and how to get a decimal answer if that’s what you’re after.

Python for Kids Book: Project 2

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 2

Project 2 is a Hello World project that covers some important basics, including what literals are, how you name a literal in order to store it (ie variables). It shows the different ways to make a string literal.If you’re going to name a value you need to know about Python’s naming rules (and PEP8 naming conventions – yah but I don’t actually mention PEP8). Those naming rules themselves rely on you not using a keyword as a name, so you also get a list of Python keywords in this project.

It also covers Python’s print statement, looping with while (including the notion of a conditional and a code block). looping with for and counting with range. By the end of the Project you can fill the screen with “Hello World!”.  To do so you repeat something 300 times. I introduce the concept of magic numbers and explain why they should be avoided. I offer the use of variables in ALL_CAPS to use as constants.

Update March 2016: The book is designed to teach you Python 2.7. However, Python 3 is on the way, and if you’d like to see what’d be different in this project if you use Python 3 see this post.

Python for Kids Book: Intro and Project 1

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 the Introduction

The introduction of the book sets out some information to help you understand how the book is written. It gives some examples of the different fonts used in the book to show code, how special terms are indicated (with italics), about the Python console. In particular, in the book some code samples occur with >>> in front of them. Where you see these you need to go to your own Python console and type into it the text which comes after the >>> . The introduction also explains some stuff about how long lines are treated and how to indent your code (important in Python).

What’s in Project 1

Project 1 of the book is not so much a project as some Python basics. In it I give you some context about what Python is and how it is used. You learn how to download and install Python, and how to start and stop the Python interpreter. You learn where to find Python documentation online and about PEPs! I also try my best to tell you to be an active learner – to do, rather than just read. Remember what Yoda said,  “do, or do not, there is no read” (or something like that). There’s no learning except by doing. Try to freestyle it if you can. The book is quite structured in what it tells you to do and what code to type and when, but please feel free to mix it up yourself. I really can’t emphasize enough how important it is to get in, try stuff out and make mistakes.

You’ve read the blog, now get the book!

Good news! I’ve been working away on a Python book for kids with the folks from Wiley. The book, called Python for Kids for Dummies is due out in a couple of weeks now.  The book is aimed at kids from about 10 and up.

See it on Amazon:

It has 13 Projects, 10 of which are in the book itself, and three will be available online from Wiley. I will be putting up more information about the projects in the coming days.