 Combining Functions and Repetitive Tasks - dummies

After you have understood the basic pieces of a language, it’s time to start putting them together in interesting ways. For example, you can create a function that can perform tasks repetitively. The goal is to make the repetitive part of the application more flexible so that it can repeat tasks as needed. A good way to view repetition is by creating a multiplication table. The following listing shows a method for achieving this goal:

```def PrintTimesTable(XStart = 1,
XEnd = 11,
YStart = 1,
YEnd = 11):
X = XStart
Y = YStart
print ('{:>4}'.format(' '), end= ' ')
for X in range(YStart, YEnd):
print('{:>4}'.format(X), end=' ')
print()
for X in range(XStart, XEnd):
print('{:>4}'.format(X), end=' ')
while Y < YEnd:
print('{:>4}'.format(X * Y), end=' ')
Y+=1
print()
Y=YStart
PrintTimesTable()
print()
PrintTimesTable(8, 12)
print()
PrintTimesTable(4, 6, 5, 8)
print()
PrintTimesTable(YStart = 5, YEnd = 9)```

The repetitive code used to create the multiplication table has been placed in a function called PrintTimesTable() that accepts a variable number of input arguments. These arguments define the starting and ending values for the rows (X) and columns (Y) of the table. The arguments are optional because they have predefined values. You can choose to define none or some of the arguments, or you can use named arguments as input, as shown in the example code. The point is that the four arguments will exist and provide values to the application.

You could have created the same example using constant values, but this example uses variable values to make the output more flexible. For example, X and Y are set to the starting values defined by XStart and YStart, respectively. The value of these variables isn’t known until runtime. An advantage of using constants is that the values are known during compile time, and Python can help you avoid potential problems with the input values.

The output part of the example begins immediately after assigning values to X and Y. To make the table readable, this example must create a heading at the top and another along the side. When users see a 1 at the top and a 1 at the side, and follows these values to where they intersect in the table, they can see the value of the two numbers when multiplied.

The first print() statement adds a space (because nothing appears in the corner of the table). All the formatting statement says is to create a space four characters wide and place a space within it. The {:>4} part of the code determines the size of the column. The format( ) function determines what appears in that space. The end attribute of the print() statement changes the ending character from a carriage return to a simple space.

The first for loop displays the numbers at the top of the table. The numbers that you see displayed depend on the YStart and YEnd input values.

At this point, the cursor is sitting at the end of the heading row. To move it to the next line, the code issues a print() call with no other information.

Even though the next bit of code looks quite complex, you can figure it out if you look at it a line at a time. The multiplication table shows the values from XStart * YStart to XEnd * YEnd, so you need X rows and Y columns to display the information. The for statement tells Python to create X rows.

The first print() call displays the row heading value. Of course, you have to format this information, and the code uses a space of four characters that end with a space, rather than a carriage return, in order to continue printing information in that row.

The while loop comes next. This loop prints the columns in an individual row. The column values are the multiplied values of X * Y. Again, the output is formatted to take up four spaces. The while loop ends when Y is updated to the next value using Y+=1.

Now you’re back into the for loop. The print() statement ends the current row. In addition, Y must be reset to 1 so that it’s ready for the beginning of the next row, which begins with 1. When you run this application, you see the four tables shown in the following screenshot. 