Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures FOR Statement Looping.

Similar presentations


Presentation on theme: "Control Structures FOR Statement Looping."— Presentation transcript:

1 Control Structures FOR Statement Looping

2 S E Q U REPITITION …or Iteration E N …a step or sequence of steps that are repeated until some condition is satisfied. C E

3 Sometimes we want to repeat a bit of code many times
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’)

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

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

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

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

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

9 What do you think the output will be for this block?
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

10

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

12

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

14 for y in range(5): print(y * '*') for y in range(5,0,-1):
Look carefully at the following source code. for y in range(5): print(y * '*') for y in range(5,0,-1): 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.

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

16 Try this for fun! ((Double-space used here and in the 1st FOR loop also))

17

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

19 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!

20 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')

21 from random import randint
Using the random number generator… 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 randint(1,100)

22 Save this as ‘Guess Random Secret Number’.
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’.


Download ppt "Control Structures FOR Statement Looping."

Similar presentations


Ads by Google