Coding - Part 3 - CYU
Coding - Part 3 - CYU
BIG IDEAS:
• Loops provide an efficient way to perform repeated calculations
• for loops repeat a specified number of times
• while loops repeat as long as a specified condition is met
Coding – Part 3
BUILD YOUR SKILLS
1. Consider the two programs shown below.
PROGRAM A PROGRAM B
Start loop Set x = 0 and y = 0
Use 5 consecutive integers for
x, starting at 0. Start loop
Repeat as long as y ≤ 20.
Set y = 2*x + 7
Set y = 2*x + 7
Print y
Print y
End loop
Set x = x + 1
End loop
2. a) Briefly explain the difference between a for loop and a while loop.
Coding – Part 3
3. Consider the Python program shown below.
4. The height of a ball (in metres), t seconds after it is thrown, is given by the equation
h = −4.9t 2 + 22.3t + 2.1 .
d) Modify your program from part (c) to print the height of the ball every 0.1 seconds, from
t = 0 until the time the ball hits the ground. Round the height values to two decimal places.
e) Using the output of your program from part (d), state the maximum height of the ball and
the time it takes to reach that height.
Coding – Part 3
5. Suppose we wish to create a Python program to solve equations of the form ax + b = c , where a, b
and c are known values.
a) Write a program that prompts the user to enter values for a, b and c in the above equation
and then solves for x.
b) For each of the following equations, state the values that would be entered for a, b and c in
your program and then use your program to solve the equation.
i) 4 x + 3 = 15 ii) −9.2 x + 1285.3 = −1861.1
iii) x − 14 = −38 iv) −32.5 x = −825.5
c) Will your program work for all values of a, b and c? Explain.
6. The following blocks represent the steps of a program that averages five numbers entered by the
user.
1 1 1
7. Consider the sum 1 + + + + ...
2 4 8
a) Create a Python program to evaluate the above sum for the desired
number of terms (for example, the sum of the first 20 terms).
b) As more terms are added, what happens to the value of the sum?
1 1 1
c) Does the sum 1 + + + + ... behave similarly? Explain.
2 3 4
Coding – Part 3
CHECK YOUR UNDERSTANDING
1. a) 5 b) Increase the value of x by 1 for the next iteration of the loop.
c) PROGRAM A: PROGRAM B:
d) The program would return an error since a value of y is needed in the condition of the loop
and a value of x is needed for the calculation within the loop.
e) The program would loop indefinitely, printing an output of 7 for each iteration of the loop. The
value of x would never change and, therefore, the value of y would become fixed at 7 after it
ANSWERS
is first calculated in the loop. The value of y would never reach 20 to stop the loop.
2. a) With a for loop, the desired number of iterations is already known (for example, repeat the
loop 20 times). With a while loop, the number of iterations depends on some other criteria (for
example, repeat the loop as many times as needed until a calculated value reaches 0).
b) PROGRAM A: PROGRAM B:
3. a) The program adds ten numbers that are entered by the user.
b) x is a “dummy variable” that is only used to define the loop. x starts with a value of 0 and
increases by 1 with each iteration of the loop. The loop stops when x reaches 10 (it doesn’t
actually run for an x-value of 10 though).
c) Yes. In order to use the sum variable in the calculation in line #7, it must already be defined.
The initial value of sum should be 0 so that it does not affect the sum of the user-entered
values.
d) In line #7, the value of sum is updated by adding the number entered by the user in the current
iteration of the loop. As a result, the sum variable is a running total of all the numbers entered
by the user.
e) The value of sum would not be updated and the program would ultimately output the original
value of sum, which is 0.
Coding – Part 3
4. a)
e) The maximum height of the ball is approximately 27.47 m, which occurs after
approximately 2.3 seconds.
5. a)
b) i) a = 4, b = 3, c = 15 and x = 3
Coding – Part 3
6. a) Set total = 0 b)
Start loop
Repeat 5 times.
Print average
ANSWERS
c)
Coding – Part 3
7. a)
Coding – Part 3