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 code (for loop ITERATION)
for loop in range (1,11,1): # for loop that will use the loop variable to control the number of time to repeat the indented code
print("Loop number ",loop) # will output "Loop number" and then the content of the loop variable
# REMEMBER The loop variable is the control for the for loop.
# the integer number that is held within the loop variable will
# start at 1, end at 11 and step (count up in) 1's
Apply your knowledge of for loops to write a program that will ask the user for 10 numbers, after the input of each number the new input will be added to a total. When all 10 numbers have been collected and a total calculated it will output the total on the screen for the user.
Input 10 numbers
Calculate the total of all 10 numbers
Output total to user
Help: Remember your using a loop so the indented code in your loop will take a number then straight away add it to the total before the next loop starts.
total = 0
for loop in range (0,10,1):
num = int(input("Enter a number :"))
total = total + num
print("The total of all 10 numbers is ",total)
Give a reason you would know you should implement a for loop in your program.
Describe why I described a for loop as scalable.
Show the output on the screen of the code below
for count in range (0,20,2):
print(count)
Describe the role of the variable count in the following code example
for count in range (1,11,1):
print(2 , "x" , count , "=" , 2 * count )
Explain how using a loop would increase the efficiency of your program.
Give a reason you would know you should implement a for loop in your program.
You would know to use iteration (a loop) in your program if you found yourself repeating large chunks of code, this would require you to identify what exactly is repeating then form a for loop around those instructions.
Describe why I described a for loop as scalable.
A for loop is scalable as you can simply change the number of repetitions in the range statement. You could change a loop that repeats 10 times to a loop that would repeat 1000,000 times by just changing the stop value. (0,10,1) to (0,1000000,1). You can change the amount of iterations / loops easily.
Show the output on the screen of the code below
for count in range (0,20,2):
print(count)
Screen output:
0
2
4
6
8
10
12
14
16
18
Describe the role of the variable count in the following code example
for count in range (1,11,1):
print(2 , "x" , count , "=" , 2 * count )
The count variable in the above program is not only providing he control for the for loop / iteration but it is also being used as part of the multiplication.
In each loop the count variable holds the next value as shown below:
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
...
2 x 10 = 20
You are performing the multiplication 10 times with 10 different values as count changes each loop / iteration.
Describe how using a loop would increase the efficiency of your program.
If you produced a program that only used a sequence of instructions and no iteration / loops, you end up having to write out the same instructions numerous times. You have just added unnecessary code to your program. Example: if you needed to repeat 5 lines of code 10 times. If you just produced a sequence of instructions with no iteration you would need to repeat the same code 10 times, making your program approx. 50 lines of code. Instead you could implement iteration in the form of a for loop. You would identify the 5 lines of code that are essential and indent them inside a for loop structure. Your program would be 6 lines of code, shortening your program.
Other than shortening your code it would have the following effects on your computer: programs would occupy less RAM and system resources, less storage space would be needed for storing program source code, faster compellation of code.