Extend Your First Python Program on Your Raspberry Pi

Your first Python program is a small piece of code to print multiplication tables. For another challenge, you can change the code a few different ways to make the program behave differently. See if you can make these changes before looking at the answer.

But before you start, be a bit creative. When you are asked for what table to produce, type something like 2.563. It still produces the goods:

This program calculates times tables
It is from Raspberry Pi For Dummies
 
Which multiplication table shall I generate for you? 2.563
 
Here is your 2.563 times table:
 
1 times 2.563 is 2.563
2 times 2.563 is 5.126
3 times 2.563 is 7.689
4 times 2.563 is 10.252
5 times 2.563 is 12.815
6 times 2.563 is 15.378
7 times 2.563 is 17.941
8 times 2.563 is 20.504
9 times 2.563 is 23.067
10 times 2.563 is 25.63
11 times 2.563 is 28.193
12 times 2.563 is 30.756

Now you are challenged to produce

  • The table surrounded by a box

  • All the tables from 2 to 12

  • A table of square numbers

  • A table of cube numbers

  • A table of square roots

Try and do these in turn before reading the solution.

Solution 1: Surround the table with a box

This might sound easy. You can start off by adding a print statement before and after the tables to give the box a top and bottom using the underscore symbol (_), and you can add a pipe symbol (the | symbol, found between Ctrl and Alt on a lot of keyboards) to each line of the table, giving you this:

# simple times table program framed
print "This program calculates a times table"
print "It is from Raspberry Pi For Dummies"
tablenum=input("\nWhich multiplication table shall I generate for you? ")
print "\nHere is your", tablenum, "times table:\n"
print "____________________"
for i in range(1,13):
    print "|",i, "times", tablenum, "is", i*tablenum,"|"
print "____________________"
print "\nHope you found that useful!"

But the problem is that the output looks like this:

Here is your 2 times table:
____________________
| 1 times 2 is 2 |
| 2 times 2 is 4 |
| 3 times 2 is 6 |
| 4 times 2 is 8 |
| 5 times 2 is 10 |
| 6 times 2 is 12 |
| 7 times 2 is 14 |
| 8 times 2 is 16 |
| 9 times 2 is 18 |
| 10 times 2 is 20 |
| 11 times 2 is 22 |
| 12 times 2 is 24 |
____________________

It doesn't line up very well. This is because for the first nine lines, there is only one digit in the first number and two after that. Also, the answer has one digit in the first four lines and two after that.

So how can you tell the computer to print out a more regular spacing? It is possible to print out a Tab character by using \t in your print statement. Armed with that, you can produce

# simple times table program framed
print "This program calculates a times table"
print "It is from Raspberry Pi For Dummies"
tablenum=input("\nWhich multiplication table shall I generate for you? ")
print "\nHere is your", tablenum, "times table:\n"
print "________________________"
for i in range(1,13):
    print "|  ",i, "times", tablenum, "is", i*tablenum,"\t|"
print "________________________"
print "\nHope you found that useful!"

And that gives an output of

________________________
|   1 times 7 is 7                                     |
|   2 times 7 is 14                                    |
|   3 times 7 is 21                                    |
|   4 times 7 is 28                                    |
|   5 times 7 is 35                                    |
|   6 times 7 is 42                                    |
|   7 times 7 is 49                                    |
|   8 times 7 is 56                                    |
|   9 times 7 is 63                                    |
|   10 times 7 is 70                                   |
|   11 times 7 is 77                                   |
|   12 times 7 is 84                                   |
________________________

That’s a lot better, but if you don’t like the gap at the bottom caused by using the underscore character, replace the underscore with the slash, to create

/////////////////////////
/   1 times 2 is 2      /
/   2 times 2 is 4      /
/   3 times 2 is 6      /
/   4 times 2 is 8      /
/   5 times 2 is 10     /
/   6 times 2 is 12     /
/   7 times 2 is 14     /
/   8 times 2 is 16     /
/   9 times 2 is 18     /
/   10 times 2 is 20    /
/   11 times 2 is 22    /
/   12 times 2 is 24    /
/////////////////////////

This is the complete code:

# simple times table program framed
print "This program calculates a times table"
print "It is from Raspberry Pi For Dummies"
tablenum=input("\nWhich multiplication table shall I generate for you? ")
print "\nHere is your", tablenum, "times table:\n"
print "/////////////////////////"
for i in range(1,13):
    print "/  ",i, "times", tablenum, "is", i*tablenum,"\t/"
print "/////////////////////////"
print "\nHope you found that useful!"

Solution 2: Print out all the tables from 2 to 12

The idea here is not to ask for what table you want to print, but to print them all from the 2 times table to the 12 times table. All you have to do is to enclose the working part of the code in a for loop and remove the tables input part. Note you have to add indentation to all the lines you want to include in the for loop. Use the final code from the last challenge to build on.

Solution 3: Produce a table of square numbers

Because there is only one table of square numbers, you don't need to ask what table is needed. All that needs to change is the line that does the calculation and prints out the result. To find a square, simply multiply the number by itself. The code is here:

# simple times table program
print "This program calculates times tables from 2 to 12"
print "It is from Raspberry Pi For Dummies"
for tablenum in range(2,13) :
    print "\nHere is your", tablenum, "times table:\n"
    print "/////////////////////////"
    for i in range(1,13):
       print "/  ",i, "times", tablenum, "is", i*tablenum,"\t/"
    print "/////////////////////////"
print "\nHope you found that useful!"

Solution 4: Produce a table of cube numbers

Given that you have just done the table of square numbers, making a cubed version is simple. Just change one line:

    print "/  ",i, "cubed is", i*i*i,"\t/"

Solution 5: Produce a table of square roots

Square roots are slightly trickier because no square root function is built into the basic instructions of Python. However, there are a few ways round this. The simplest is to use the exponent function. That raises a number to a power. If you paid attention at school, you remember that a number raised to the power of a half gives the square root.

The exponent function in Python is a double star (**), so change the calculation line to

print "root", i, "is", i ** (0.5)

The other way to do this is to get some help from a Python module. There is one built-in called math, but in order to use it, you need to import it at the start of your code. You then have a square root function at your disposal that you can use in place of the exponent.

However, if you run the code, you see that some square roots are printed with lots of decimal places. There is an old adage that “After the third decimal place, nobody gives a damn.” This is not always true, but for the sake of learning a table, it helps.

Therefore, limit the number of places the program prints by using the round function. This takes in two numbers. The first is the number you want to round and the second is the number of decimal points you want to round it to. The complete code is here:

# simple root table program
import math
print "This program calculates a root table"
print "It is from Raspberry Pi For Dummies"
print
print "////////////////"
for i in range(1,13):
    print "root", i, "is", round(math.sqrt(i),3)
print "////////////////"
print "\nHope you found that useful!"
blog comments powered by Disqus
Advertisement

Inside Dummies.com