O'Reilly logo

Raspberry Pi For Dummies by Mike Cook, Sean McManus

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

Creating the Times Tables Program

Now we’re going to show you how to make a program that generates multiplication tables. For example, if the user requests a multiplication table for the number 7, it outputs the sequence 7, 14, 21, and so on. The program is only a few lines long, but it teaches you how to create programs, how to use variables to store numbers, how to ask the user for information, and how to create sections of program that repeat (loops). You’ll build on your understanding of the print command to do all this, and if you’ve read Chapters 10 and 11 on Scratch, some of the ideas should be familiar to you.

Creating and running your first Python program

The problem with entering instructions in the shell is that you have to enter them each time you want to use them. The commands are carried out straight away, too, which limits the sophistication of the kinds of things you can do. You can solve these problems by creating a program, a set of repeatable instructions that you can save as a file and use again.

To create a program, you use script mode, which is a blank window when you open it, but otherwise looks like Figure 12-2. To open the script mode window, click the File menu at the top of the Python shell, and then click New Window.

When you enter commands in script mode, they’re not carried out straight away. The window acts like a simple text editor, and enables you to enter your list of commands (or program), and gives you control over when those commands are carried ...

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