Special Cases and Gotchas
The easiest mistake is getting the amount of padding wrong, causing the columns to no longer be
aligned. For example, if you fail to pad the single-digit products with an extra space character, the
Python Programming Exercises, Gently Explained
78
multiplication table ends up looking like the misaligned table in the previous section.
Now try to write a solution based on the information in the previous sections. If you still have
trouble solving this exercise, read the Solution Template section for additional hints.
Solution Template
Try to first write a solution from scratch. But if you have difficulty, you can use the following
partial program as a starting place. Copy the following code from https://invpy.com/multiplicationtable-
template.py and paste it into your code editor. Replace the underscores with code to make a working
program:
# Print the heading of each column:
print(' | 1 2 3 4 5 6 7 8 9 10')
print('--+------------------------------')
# Loop over all numbers from 1 to 10:
for column in range(____, ____):
# Print the number label on the right side:
print(str(____).rjust(____) + '|', end=____)
# Loop over all numbers from 1 to 10:
for row in range(____, ____):
# Print the product, padded to two digits, followed by a space:
print(str(____).rjust(____) + ' ', end=____)
# After the loop, print a newline to end the row:
____()
The complete solution for this exercise is given in Appendix A and
https://invpy.com/multiplicationtable.py. You can view each step of this program as it runs under a
debugger at https://invpy.com/multiplicationtable-debug/.
Further Reading
A similar multiplication table project is also used in the free book, The Big Book of Small Python
Projects, as Project #49 at https://inventwithpython.com/bigbookpython. If you are interested in chemistry,
that book also has a project that displays the periodic table of elements.
|