Special Cases and Gotchas
The order of the nested for loops is important. You want the innermost for loop to iterate
over minutes, the next innermost to iterate over hours, and the outermost for loop to iterate over
'am'
and 'pm'.
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/every15minutes-
template.py and paste it into your code editor. Replace the underscores with code to make a working
program:
# Loop over am and pm:
for meridiem in [____, 'pm']:
# Loop over every hour:
for hour in [____, '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']:
# Loop over every 15 minutes:
for minutes in ['00', ____, ____, '45']:
# Print the time:
print(____ + ':' + ____ + ' ' + ____)
The complete solution for this exercise is given in Appendix A and
https://invpy.com/every15minutes.py. You can view each step of this program as it runs under a debugger
at https://invpy.com/every15minutes-debug/.
76
E X E R C I S E # 2 5 : M U L T I P L I C A T I O N T A B L E
Learning the multiplication table is an early part of our childhood math education. The
multiplication table shows every product of two single digit numbers. In this exercise, we print a
multiplication table on the screen using nested for loops and some string manipulation to align the
columns correctly.
Exercise Description
Write a program that displays a multiplication table that looks like this:
| 1 2 3 4 5 6 7 8 9 10
--+------------------------------
1| 1 2 3 4 5 6 7 8 9 10
2| 2 4 6 8 10 12 14 16 18 20
3| 3 6 9 12 15 18 21 24 27 30
4| 4 8 12 16 20 24 28 32 36 40
5| 5 10 15 20 25 30 35 40 45 50
6| 6 12 18 24 30 36 42 48 54 60
7| 7 14 21 28 35 42 49 56 63 70
8| 8 16 24 32 40 48 56 64 72 80
9| 9 18 27 36 45 54 63 72 81 90
10|10 20 30 40 50 60 70 80 90 100
The number labels along the top and left sides are the numbers to multiply, and where their
column and row intersect is the product of those numbers. Notice that the single-digit numbers are
padded with spaces to keep them aligned in the same column. You may use Python’s rjust() string
method to provide this padding. This method returns a string with space characters added on the left
side to right-justify the text, and the Solution Design section explains how it works.
The line along the top side of the table is made up of minus sign characters. The line along the
left side is made up of vertical pipe characters (above the Enter key on the keyboard). A plus sign
marks their intersection. Your solution is correct if the output matches the above text of the
multiplication table. You can use a simple print() call for the number labels and lines at the top of
the table. However, don’t hard code the text of the multiplication table into your program: your
program should be more than just a bunch of print() calls.
Try to write a solution based on the information in this description. If you still have trouble
solving this exercise, read the Solution Design and Special Cases and Gotchas sections for
additional hints.
Prerequisite concepts: print(), for loops, range() with two arguments, end keyword
argument for print(), rjust(), str()
Python Programming Exercises, Gently Explained
77
Dostları ilə paylaş: |