Watch the video and take notes, pay attention to how key instructions can be repeated.
Run and walk through the example (in code window).
Read the task and write code to solve.
Make a note of successful code in your book.
Answer questions in your book.
Watch the video and create notes in order to be able able to apply the knowledge given.
Example 1:
# Code example (while loop ITERATION)
num = 1 # developer sets control variable and assigns value
while num < 11: # while loop with condition, condition must be True to loop, when False will stop
print("Inside the loop, loop number ",num) # outputs the value held in num
num = num + 1 # increments the value in num, every loop adds one to the number held within
# REMEMBER: The num variable holds the value that will stop the loop, you are adding 1 to the value.
# When the value reaches anything that isn't < 10 then the while loop condition is False.
# IMPORTANT: If the num variable was not incremented then the loop would never stop (infinite loop)
# as the while loop conditional statement num < 10 would never be False.
Example 2:
# Code example (while loop ITERATION)
repeat = True # Variable that holds the value True
while repeat == True: # Use the repeat variable as the control in our conditional statement. Repeat is True to the loop with function
name = str(input("What's your name : (type exit to stop loop)"))
if name == "exit":
repeat = False
else:
print("Hello", name ,"hope your having a nice day!")
Apply your knowledge of while loops to write a program that will allow a user to enter numbers, each number input will be subtracted from a total of 100. While the value in the total variable is greater than 0 continue to input numbers. When the total value is less than or equal to 0 output how many numbers were entered.
Repeat while total is greater than 0
input number from a user
subtract number from total
count number of inputs
output number of inputs taken for total to be less than or equal to 0
total = 100
countup = 0
while total > 0:
num = int(input("Enter a number to subtract :"))
total = total - num
countup = countup + 1
print("It took ",countup,"numbers to stop the loop")
State the difference between the two loops for and while
Describe why a while loop is known as a condition controlled loop
Describe the term "infinite loop"
Give the variable number a value that would stop the loop below:
while number > 1
Describe how computer games that use lives (pacman) would use a while loop to function
Describe the difference between the two loops for and while
A for loop is controlled and monitored by the computer as the conditions are set when the loop starts. A while loop is controlled by a Boolean expression/conditional statement that when its True the loop repeats, this means that a while loop when it begins has no set end point and can repeat indefinitely.
Describe why a while loop is known as a condition controlled loop
A while loop only repeats its indented code while the condition that is created by the developer is True, when the condition is False the indented code is stopped.
Describe the term "infinite loop"
An infinite loop is when a while loops conditional statement can never be met, it will always be True as their is no code inside the loop to ensure it turns to False.
Give the variable number a value that would stop the loop below:
while number > 1
0 or a negative number like -1 would stop the while loop from repeating.
Describe how computer games that use lives (pacman) would use a while loop to function
while lives > 0
pacman game code is here and repeats while the player has a value greater than 0 in the lives variable