control structures for statement looping. s e q c e n u e repitition …a step or sequence of steps...

22
Control Structures FOR Statement Looping

Upload: shawn-richard

Post on 29-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Control Structures

FOR Statement

Looping

Page 2: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

S

E

Q

C

E

N

U

E

REPITITION…a step or sequence of steps that are repeated until some condition is satisfied.

…or Iteration

Page 3: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Sometimes we want to repeat a bit of code many times. We use loops to achieve this!

For example, if you wanted to output the same statement 3 times, there are 2 ways we could do this.

METHOD 1

print(‘Hello World’)print(‘Hello World’)print(‘Hello World’)

Page 4: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Method 2: Use a Loop

In Python, there are 2 looping mechanisms…

FOR LOOP

WHILELOOP

LOOPS for a specified number of times!

For example, output HELLOW WORLD 3 times

We will study later

Page 5: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

>>> for x in range (0,3):print('Hello World')

The Range Function is useful here as it enables us to specify a start value and an end value.

Range ( <start>, <stop>)((Note: start is optional, otherwise it starts at 0))

Pay careful attention to the SYNTAX. Remember the amount of compile issues you had with the colon in our IF…ELSE statement ???...well, note that the FOR statement uses one too.

Page 6: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

>>> for x in range (1,5):print('Hello World')

How many times will Hello World be output?

Every time a loop is made, we call this an iteration.

There were 4 iterations in the command above.

Try entering the following into your interpreter:

4 times (it STOPS at the 5th count)

>>> for x in range (5000):print(x)

Page 7: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Look at the following examples together:

What will be the output of the following?

1.

2.

3.

Page 8: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

When learning looping, it’s useful to count the number of iterations (loops). As this FOR loop iterates, the variable ‘x’ increments by 1. We can simply output the value of ‘x’ for each iteration! For example,

For x in range(5):print(‘This is iteration number: ‘, x)

This will output…

This is iteration number: 0This is iteration number: 1This is iteration number: 2This is iteration number: 3This is iteration number: 4

Page 9: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Sometimes, we can put a loop inside a another loop! WOW! These are known as Nested Loops.

Things seem to start getting a little complicated, but the mechanism is exactly the same.

for x in range(1,3):print(‘This is OUTER loop statement ’, x)for y in range(1,4):

print(‘This is INNTER loop statement - - ‘,y)

What do you think the output will be for this block?

This FOR loop will iterate (1,3) i.e. twice

This FOR loop will iterate (1,4) i.e. thrice

Page 10: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied
Page 11: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Look at the following source code.

for x in range(1,4): print('This is OUTER loop number: ', x) for y in range(1,3): print('This is INNER loop number: ',x,' , ',y)

On a separate sheet of paper (file paper will do) write down the output expected from Python for this statement. Be exact as you can in your answer.

(Remember that range(1,4) means ‘starting at 1 and stop at 4’ …giving you 3 iterations)

Page 12: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied
Page 13: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Look at the following source code.

for y in range(5): print(y * '*')

On your same separate sheet of paper (file paper will do) write down the output expected from Python for this statement. Be exact as you can in your answer.

Page 14: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Look carefully at the following source code.

for y in range(5): print(y * '*')

for y in range(5,0,-1): print(y * '*')

Explain to your teacher what you think is going to be output here…

Tip: in the second block of code, note the starting value of ‘y’!

This means, decrement by 1 after each iteration.

Page 15: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Try this for fun – change the number of iterations to 50 for both ranges.

Page 16: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Try this for fun!

((Double-space used here and in the 1st FOR loop also))

Page 17: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied
Page 18: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

The break statement

Sometimes you might want to stop iterating through a loop. We can place a break statement where we want our programs to terminate.

Page 19: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

TASK Guess the secret number!•Prompt the user for their name. •Ask them to guess a number between 1 to 10.•Give them 5 attempts to guess the correct number!

•REFINEMENT 1:•Improve your program by stopping the iterations if they guess correctly (You will need to use a break statement)

•REFINEMENT 2:•Improve your program by giving them their total guess count along with their name.

For example, Well done Jude! You guessed correctly! Unfortunately it took you [guess_count] guesses!

Page 20: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

my_number = 7their_guess = 8for x in range(1,5): their_guess = int(input('Please enter a guess between 1 and 10')) if their_guess == my_number: print('Well done') break else: print('Hard luck')

Page 21: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Using the random number generator…

from random import randint

We can ‘import’ very useful ‘modules’ which can give extra functionality to our programs. One such module is the ‘random’ module. This is how we import it….

…then we can ask it to generate a random number for us between 1 and 100…

from random import randintrandint(1,100)

Page 22: Control Structures FOR Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied

Using the random number generator…

Copy your ‘Guess Secret Number’ game so that it uses the randomiser to pick a random number between 1 and 10, rather than you setting it at design time.

Save this as ‘Guess Random Secret Number’.