PROGRAMMING
CONSTRUCT
SEQUENTIAL
SELECTION (IF..THEN, IF..THEN..ELSE, IF..THEN..ELSEIF) LOOP
NESTED IF – HOMEWORK SAMPLE
SOLUTION
STARTER: HOW CAN YOU DEVELOP A
PROGRAMMING SOLUTION TO THIS PROBLEM?
Write a program to accept five scores and output the total.
num1 = int(input(“Enter number 1”))
num2 = int(input(“Enter number 2”))
num3 = int(input(“Enter number 3”))
num4 = int(input(“Enter number 4”))
num5 = int(input(“Enter number 5”))
total = num1 + num2 + num3 + num4 + num5
print (“The sum is”, total)
1. Does the solution solve the problem?
2. What are the steps (the algorithm) involved?
3. How could you refactor this program to avoid repeating five nearly
identical input() lines?
STARTER: HOW CAN YOU DEVELOP A PROGRAMMING
SOLUTION TO THIS PROBLEM? USE A LOOP TO ACCEPT INPUT
AND COMPUTE THE RUNNING TOTAL
total = 0
for counter in range(0, 5):
num1 = int(input(“Enter number 1”)) num = int(input(f"Enter number {i+1}: "))
num2 = int(input(“Enter number 2”)) total = total + num
num3 = int(input(“Enter number 3”)) print("The sum is", total)
num4 = int(input(“Enter number 4”))
num5 = int(input(“Enter number 5”)) Naturally: loop counter from 1 to 5
total = num1 + num2 + num3 + num4 + num5 Python: for counter in range(0, 5):
print (“The sum is”, total)
Loops are fundamental/basic
programming constructs that allow for
the repeated execution/running of a
block of code
LOOPS
• Loop allows for the repeated execution/running of a block of code
• Types of loop:
1. Counter-controlled
2. Pre-condition
3. Post condition
COUNTER-CONTROLLED LOOPS:
• In a counter-controlled loop, the number of iterations is known in
advance.
• These loops typically involve an initialization of a counter variable, a
condition that checks the counter against a limit, and an
increment/decrement of the counter.
• Example: The FOR loop in many programming language
USE A TRACE TABLE TO TEST THE LOGIC OF THE
PROGRAM AND TO DETERMINE THE OUTPUT
Test data: 6, 5, 10, 20, 3
total = 0
for counter in range(0, 5):
num = int(input(f"Enter number {i+1}: "))
total = total + num
print("The sum is", total)
total counter num Input () output ()
COUNTER-CONTROLLED
• In a counter-controlled loop, the number of iterations is known in advance.
• These loops typically involve an initialization of a counter variable, a condition that
checks the counter against a limit, and an increment/decrement of the counter.
# Example 1: Loop a specific number of times (e.g., 5 times)
for i in range(5):
print(f"Iteration number: {i}")
# Example 2: Loop from a starting value to an ending value (exclusive)
for j in range(1, 6): # This will iterate for j = 1, 2, 3, 4, 5
print(f"Current value: {j}")
# Example 3: Loop with a step value
for k in range(0, 10, 2): # This will iterate for k = 0, 2, 4, 6, 8
print(f"Stepped value: {k}")
INDIVIDUAL WORK- 10 MINS
See worksheet
# PROGRAM TO PRINT SQUARES OF NUMBERS FROM 1 TO 5
for i in range(1, 5):
print("The square of", i, "is", i^2)
There are three errors in this program. Identify the errors and suggest a
correction.
Error #:
Correction:
CORRECTION
# Program to print squares of numbers from 1 to 5
for i in range(1, 6): # fix: include 5
print ("The square of", i, "is", i**2)
# fix: correct operator and indentation
Seating
plan-Term 1
Classwork/Homework
Write a Python program that asks the user to enter the rainfall (in mm) for 7 days.
After all inputs are entered, the program should calculate and display:
The total rainfall for the week.
The average rainfall.
The highest rainfall in a single day.
The lowest rainfall in a single day.
The program above attempts to solve the given problem, but there is
one error in the upper section of the code. Identify the error and
suggest a correction.
highest_rainfall = -1
lowest_rainfall = 99999
for day in range(1, 8):
rain = float(input("Enter rainfall for day " + str(day) + " (in mm): "))
total_rainfall += rain
if rain > highest_rainfall:
highest_rainfall = rain
if rain < lowest_rainfall:
lowest_rainfall = rain
average_rainfall = total_rainfall / 7
print("Total rainfall:", total_rainfall, "mm")
print("Average rainfall:", round(average_rainfall, 2), "mm")
print("Highest rainfall in a single day:", highest_rainfall, "mm")
print("Lowest rainfall in a single day:", lowest_rainfall, "mm")
Complete the worksheet
1. Construct and extend the
trace table to understand
the logic of the problem
based on rainfall data
2. Use programming
techniques learned to
develop programs.
3. Upload your work
Upload your work here
PAIR WORK: HOW CAN YOU DEVELOP A PROGRAMMING SOLUTION TO THIS PROBLEM?
John is the leader of his tennis club, which has a total of 10 members, including
6 existing members and 4 new members.
• Returning members pay a full membership fee of $20 each.
• New members receive a 20% discount
Write a program to accept status of members (1: New 2: Existing) calculate and
display the total membership fees collected from all members.
Algorithm
initialize total variable to zero
loop COUNTER from 1 to 10
accept status
set fee as 20 for existing members
or calculate/set discounted fee for new members
add the fee to the existing total
Display the total fees collected
Sample code
1. Execute the
program
2. Test your program
with the following
data
Test case 1:
1,1,1,1,2,2,2,2,2,2
Test case 2:
1,1,1,1,1,1,2,2,2,2
Discuss the logic
error
How could you validate the user’s input so that the program accepts no more than 4
discovered
new members and 6 existing members, ensuring the total always adds up to 10
following
members? test case 2.
What would you change, what would you add? Homework: Extend your program to
allow for validation
Sample
output