Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

ICS3C Unit 2 - Programming (Module III)

Code

Uploaded by

Hockey for life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

ICS3C Unit 2 - Programming (Module III)

Code

Uploaded by

Hockey for life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAMMING

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()

print("Press CTRL-C to stop the following message.")


sleep(0.500)

# 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.

What is the result of print()? __________________________________________

Remember to complete the program header (see below) and re-save:

Filename: pr3_1.t
Description: A message is repeated until CTRL-C is pressed.

Enter your name and today’s date. Input: none


Output: messages
_____________________________________________________________________________________
Page 3-2
ICS20 Programming

Note the standard form for the loop statement is:

while True:
command
command
command

2. Starting from template, write a program called pr3_2 that will:


(a) display the message Who let the dogs out?
(b) add a second line that contains the word WOOF 4 times
(c) modify your program so that the message repeats until the user presses CTRL-C
(d) modify your program so that the message Press CTRL-C to stop the
following message. will be displayed at the appropriate time.

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)

What does this program do? _____________________________________________

Experiment with the sleep commands by changing the numbers.

In the space below, explain why a sleep is necessary:

_____________________________________________________________________________________
Page 3-3
ICS20 Programming

Now place the egg commands in a loop as follows:

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)

What does this program do? _____________________________________________

Complete the program header and declaration. Save under the same filename.

For each of the following, remember to fill in the program header completely:

4. Create a program called pr3_4 that will:

(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.

(b) Modify your program by adding a loop.

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

Write the program body that you used:

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

You might also like