Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE

## What am I Learning today?

 I am learning how to use non determinite loops to repeat code until a condition is met.

## Lesson

1. Set your Learning Objectives to red.

2. We are going to save all of our work for this topic into this PowerPoint.

3. #### To download the PowerPoint by clicking on the image below

4. Save your PowerPoint as 'Programming 1'

## Writing a while loop

While we can use a While loop as a counting loop we tend to use a for loop for that, so why do we need a while loop>

A while loop is generally used for two thing.

1. To repeat a piece of code until the user gives us the answer we want.
2. To run a piece of code forever, like in a game when you don't want you program to stop unless you tell it to.

We are now going to create a simple while loop that repeats until the user answers 'yes' and then prints out 'thank you' when it leaves the while loop.

• Here is the code for you:
1. Save the file name as while_loop.

2. Run the code.

### Activity 1

1. Copy the code above into your python window.
2. Run the code and save the file name as while_loop.

### Extension Activity 1

1. Change the code so that the user is asked if they want to stay in the loop and that the loop only ends if the user answers 'no'.

• When you are finished don't forget to save your powerpoint

• We are now going to use a while loopto see if the user can guess a number between 1 and 10.

Lets make an algorithm to solve this problem

1. Step one we need to create a variable with the number they have to guess.
2. Step two we need to create a variable to store their guess.
3. Then we need to ask the user to make a guess.
4. We then need to check if the guess was correct and if it was not then ask them again
5. If the guess is corect we need to say well done and tell them what the number was.

### Activity 2

1. Copy the code above into your python window.
2. Run the code and save the file name as guess_the_number.
3. Add a count variable to the code so that it also prints out the number of guesses it took.

### Extension Activity 2

1. If the user enters a number that outside of the guessing range 1 to 10 give them a warning message saying number you entered was not between 1 and 10.
2. Give the user a clue by informing them if the number they entered was higher or lower than the number they are trying to guess
3. Change the range so it's bigger than 1 to 10.

• When you are finished don't forget to save your your powerpoint

• Look at the example code below to see how you can use a while loop to improve your game

### Summing it all up

Lets look at the learning outcomes and decide which one best describes our current level of understanding :

Tick the one you feel is closest to your level

## Learning Outcomes I need to learn how to use non determinite loops to repeat code until a condition is met.

• I have a basic understanding of how I can use non determinite loops to repeat code until a condition is met. with a little help from my teacher
• I can show my teacher that I can use non determinite loops to repeat code until a condition is met. without their help.
• I can use non determinite loops to repeat code until a condition is met. independently and I can also explain it to others and can complete any extension tasks I am given.