ICS3C Unit 2 - Programming (Module III)
ICS3C Unit 2 - Programming (Module III)
MODULE III
REPETITION
ICS20 Programming
Learning Goals –
Module III to be able to identify
situations in which looping
Repetition structures are required
During the writing of your first programs, we used what is known as sequence logic
modules. They are called sequence because the instructions follow one after the other,
i.e. in a sequence.
The true power of the computer comes with the ability to do the same task over and over
again without tiring or producing errors. The feature that causes a set of instructions to
be executed over and over again is called repetition or looping.
1. Open template and save as pr3_1. Enter the following in the program body, save and
run the program:
cls()
# start loop
while True:
print("They love me!")
print("They love me not!")
print()
sleep(0.500)
The above program will repeat the commands between the first statement of the loop
(while True:) and the last statement of the loop (sleep(0.500)) as long as the user
doesn’t stop the program (by pressing CTRL-C). Once you press CTRL-C (which will
“break” the program), the program will halt execution.
Notice that the commands that are to be repeated are indented. This allows the reader of
the program to see very easily which commands are repeated, and more importantly, it
also lets Python know what is and is not included inside the loop.
Filename: pr3_1.t
Description: A message is repeated until CTRL-C is pressed.
while True:
command
command
command
Bonus: Add appropriate delays between the WOOFs (think of the timing of the song)
If you had problems with pr3_2 then repeat it using another message. Start from
template. Save as pr3_2a.
3. Open template, save as pr3_3. Enter the following in the program body then save and
run the program:
setscreen()
drawfilloval(300, 180, 50, 50, "black")
sleep(0.500)
drawfilloval(300, 180, 50, 50, "green")
sleep(0.500)
drawfilloval(300, 180, 50, 50, "cyan")
sleep(0.500)
drawfilloval(300, 180, 50, 50, "pink")
sleep(0.500)
drawfilloval(300, 180, 50, 50, "purple")
sleep(0.500)
_____________________________________________________________________________________
Page 3-3
ICS20 Programming
setscreen()
while True:
drawfilloval(300, 180, 50, 50, "black")
sleep(0.500)
drawfilloval(300, 180, 50, 50, "green")
sleep(0.500)
drawfilloval(300, 180, 50, 50, "cyan")
sleep(0.500)
drawfilloval(300, 180, 50, 50, "pink")
sleep(0.500)
drawfilloval(300, 180, 50, 50, "purple")
sleep(0.500)
Complete the program header and declaration. Save under the same filename.
For each of the following, remember to fill in the program header completely:
(a) Draw a filled-in circle in red with centre (200, 200) and radius 50. Then draw a
filled-in circle in red with centre (200, 200) and radius 60. Then draw a filled-in
circle in red with centre (200, 200) and radius 70. Then draw a filled-in circle in red
with centre (200, 200) and radius 80. Then draw a filled-in circle in red with centre
(200, 200) and radius 90. Remember the delays between each.
Does your loop work? If you think not then read on. Your loop is actually working but
you are not getting the result that you wanted so you have what is called a logic error.
To check that your loop is working, make each ball a different colour and make the
delays longer so that you can see what is happening.
Now change the colour back to red. When the red ball of radius 90 is drawn, it will hide
all the smaller red balls when it draws them again during the second time through the
loop. To solve the problem, we need to make the red ball of radius 90 disappear. Do
you remember how to do this? After you draw the red ball of radius 90 then add a
command that will draw it in the background colour.
_____________________________________________________________________________________
Page 3-4
ICS20 Programming
5. Create a program called pr3_5 that draws a face that has 2 eyes, a nose and a mouth.
(a) modify your program so that the left eye keeps winking until the user presses a
key; save under the same filename and run it
6. Open face1. Modify your program so that both eyes blink at the same time until the user
presses CTRL-C. Save as pr3_6.
(a) modify your program so that the eyes wink alternately until the user presses
CTRL-C; save as pr3_6a.
_____________________________________________________________________________________
Page 3-5