O'Reilly logo

Python For Kids For Dummies by Brendan Scott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

PROJECT 5

A Better Guessing Game

In this project you read about one of the main parts of programming — functions. You take that knowledge and convert your Project 3 guessing game from Project 3 so that it uses functions, which will allow you to structure the program in a more easily understood way.

image

As you work through this project, you’ll see how to communicate to and from a function, as well as the transdimensional nature of variables. You also put together a function to confirm that the user wants to quit a program. You can reuse this function in your future projects. Reducing, recycling, and reusing — it’s all coming together here.

Handle Your Functions

Functions are a simple way to group together actions. With them you can do some groups of actions repeatedly without having to retype all the code. You save typing, and it’s easier to think about how to structure your program and to update it.

In Project 3, you had to retype the guessing game each time you wanted to run it — boring. You can avoid that by using what you read about in Project 4 — save the code to a file and run the file repeatedly. Or you can wrap all the code in a while clause.

One problem with these approaches: They don’t let you reuse code in other circumstances. Besides comments, which get messy as programs get larger, you have no obvious way to tell what part of the code does what.

Think of when your ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required