Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.

Similar presentations


Presentation on theme: "Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display."— Presentation transcript:

1 Iteration

2 Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display 2 etc. Stop  How can we do this? number = 1 while the number <= 1000 display the number increase the number 2 of 16

3 Review: Display Numbers up to 1000  So here is some pseudo code to display all the numbers up to 1000: Start number = 1 while number <= 1000 display number number = number + 1 Stop Why do we need to do this? 3 of 21 3 of 16

4 Review: Display Numbers up to 1000 What is happening here? 4 of 21 4 of 16

5 Python: The while loop  In Python we use the while loop for iteration We start by declaring and initializing a number variable This tells Python to execute the following code block while the condition is true This colon is essential, it indicates the start of a block of code. These tabs indicate the while loop code block – just like with conditions. Try this This line is essential, why? 5 of 16

6 Exercise Now change your code so that it 1. Displays numbers from 1 to 20 2. Displays numbers from 0 to 40, counting in two’s 3. Displays numbers from 10 down to 0 4. Displays numbers from 20 down to 0 in two’s Save all these as separate programs so that you have them for later reference 6 of 16

7 Review: Get Five Numbers  Problem: Allow the user to enter 5 numbers Start count = 0 while count < 5 get number count = count + 1 Stop Why < not <= ? 7 of 16

8 Review: Get Five Numbers 8 of 21 8 of 16

9 Review: Total Five Numbers  Now let’s also display the total of those numbers Start count = 0 total = 0 while count < 5 get number count = count + 1 total = total + number display total Stop Here is the pseudo code to ask the user for five numbers We need a total, initially set to zero We need to add the number to that total At the end, we need to display the final total 9 of 16

10 Total Five Numbers Review: Total Five Numbers 10 of 16

11 Python: Total Five Numbers Start count = 0 total = 0 while count < 5 get number count = count + 1 total = total + number display total Stop Pseudo Code Try this 11 of 16

12 Your Turn  Write a program to: Ask the user for ten numbers Display the total and average of these numbers 12 of 21 12 of 16

13 How Many Numbers?  Let’s change the program so that the user tells us how many numbers they want to enter… How does the pseudo code change? Start get maxNumbers count = 0 total = 0 while count < maxNumbers get number count = count + 1 total = total + number average = total / maxNumbers display average display total Stop 13 of 21 13 of 16

14 How Many Numbers? 14 of 21 14 of 16

15 Your Turn: Times Tables  Write a program to display a times table  Display it in the format: 1 x 12 = 12 2 x 12 = 24 etc.  Display “The end” after the times table has been fully shown  Extension: Ask the user which times table to display and how many multiples to go up to  There are at least five trip hazards in there! 15 of 21 15 of 16

16 The while loop  We don’t just have to use a simple counter to control our loop, we can use any Boolean expression  Have you ever driven your parents crazy on a car journey asking “When will we get there?”  Try this 16 of 21 16 of 16

17 Let’s look more closely… The sentry variable controls access to the loop. It must be initialized. 1.Try this code 2.Try it without the sentry variable initialization 3.Try it without the colon 4.Try it without the tab 5.Try it with a tab before We ask a question and put the response into the sentry variable We check that response does not contain: “We’re here” 17 of 21 17 of 16

18 Challenge: Guess My Number  Guess my Number Generate a random number between 1 and 100 Ask the user to guess this number Tell them if their guess is too high or too low Count how many attempts they make before they guess it correctly When they get it right, display a suitable message and tell them how many attempts it took 18 of 21 18 of 16

19 Summary  We have looked at loops of increasing complexity  We have related the pseudo code to Python programs  We have learned how to count, total and average using loops  We have learned how to let the user’s input control a loop 19 of 21 19 of 16


Download ppt "Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display."

Similar presentations


Ads by Google