Control Structures FOR Statement Looping S E Q

  • Slides: 22
Download presentation
Control Structures FOR Statement Looping

Control Structures FOR Statement Looping

S E Q U E N C E REPITITION …or Iteration …a step or

S E Q U E N C E REPITITION …or Iteration …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

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

Method 2: Use a Loop In Python, there are 2 looping mechanisms… WHILE LOOP

Method 2: Use a Loop In Python, there are 2 looping mechanisms… WHILE LOOP FOR LOOPS for a specified We will study later number of times! For example, output HELLOW WORLD 3 times

>>> for x in range (0, 3): print('Hello World') The Range Function is useful

>>> for x in range (0, 3): print('Hello World') The Range Function is useful here as it enables us to specify a start 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.

>>> for x in range (1, 5): print('Hello World') How many times will Hello

>>> for x in range (1, 5): print('Hello World') How many times will Hello World be output? 4 times (it STOPS at the 5 th count) 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: >>> for x in range (5000): print(x)

Look at the following examples together: What will be the output of the following?

Look at the following examples together: What will be the output of the following? 1. 2. 3.

When learning looping, it’s useful to count the number of iterations (loops). As this

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 This is is iteration iteration number: number: 0 1 2 3 4

Sometimes, we can put a loop inside a another loop! WOW! These are known

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. This FOR loop will iterate (1, 3) i. e. twice for x in range(1, 3): print(‘This is OUTER loop statement ’, x) for y in range(1, 4): This FOR loop will iterate (1, 4) i. e. thrice print(‘This is INNTER loop statement - - ‘, y) What do you think the output will be for this block?

Look at the following source code. for x in range(1, 4): print('This is OUTER

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)

Look at the following source code. for y in range(5): print(y * '*') On

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.

Look carefully at the following source code. for y in range(5): print(y * '*')

Look carefully at the following source code. for y in range(5): print(y * '*') This means, decrement by 1 after each iteration. 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’!

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

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

Try ! n u f r o this f ((Double-space used here and in

Try ! n u f r o this f ((Double-space used here and in the 1 st FOR loop also))

The k a bre Sometimes you might want to stop iterating through a loop.

The k a bre Sometimes you might want to stop iterating through a loop. We can place a break statement where we want our programs to terminate. ent m e t a t s

TASK Guess the secret number! • Prompt the user for their name. • Ask

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!

my_number = 7 their_guess = 8 for x in range(1, 5): their_guess = int(input('Please

my_number = 7 their_guess = 8 for 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')

Using … r o t a r e n e rg e b m

Using … r o t a r e n e rg e b m u n m o d n a r e h t 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…. from random import randint …then we can ask it to generate a random number for us between 1 and 100… from random import randint(1, 100)

Using … r o t a r e n e rg e b m

Using … r o t a r e n e rg e b m u n m o d n a r e h t 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’.