Control Flow and Functions
English translation
Learning objectives
A single boundary value changes the decision. Turn a conditional into a function and compare ordinary and boundary inputs.
Conditions and loops
Starting conditions
Scores=[45,60,75,90], and the passing criterion is 60 or higher.
Procedure
- Use for and if to determine whether each score passes.
- Implement the same process as pass_flags(scores,threshold) and compare the two results.
Results to retain
- A 4-row table and a reusable function.
scores=[45,60,75,90]
def pass_flags(values,threshold=60):
return [v>=threshold for v in values]
print(pass_flags(scores))
Record results and self-check
Score:
Loop result:
Function result:
Match:
Points to check
- Did you actually test the boundary value 60?
Changing the Threshold
Starting conditions
Compare passing-student counts and boundary samples at threshold=50,60,80.
Procedure
- Call
pass_flagswith each Threshold in . Keep the four scores unchanged. - Tabulate the four pass/fail decisions and the passing-student count for each Threshold. Check whether the comparison operator used when a sample's score equals the Threshold is
>=. - Identify scores whose decisions changed with the Threshold and explain why.
Results to retain
- A table of decisions and passing-student counts by Threshold.
thresholds=[50,60,80]
Record results and self-check
Threshold:
Decisions for the four scores:
Number of students who pass:
Scores whose decisions changed:
Points to check
- Did you record the decision for a value equal to the cutoff score?
Checking Other Inputs
Starting conditions
Check the function's boundary values using scores [59,60,61] and an empty List.
Procedure
- Keep Threshold 60 and write the expected function return values for scores
[59,60,61]and an empty List before running. - Call
pass_flagson both inputs and record expected and actual return values side by side. Explain the decision for score 60 and the form of the return value for an empty input.
Results to retain
- Expected and actual return values for both inputs, and an explanation of the boundary value.
# Use the preceding activity's data and functions to check the specified new examples.
Record results and self-check
Input scores:
Prediction before running:
Actual return value:
Boundary value and empty input handling:
Points to check
- Did you run the same function on an empty input too?