Python - Objective 02 Learn How To Use Selection Workbook
Python - Objective 02 Learn How To Use Selection Workbook
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
TRY
Sample program:
# Selection commands
# Main program
CheckAge(14)
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
INVESTIGATE This program outputs you are old enough to drive a car if Age is
greater than or equal to 17.
# Main program
CheckAge(14) • Tabs are used to indicate which sections of code to execute.
• Code in the “else” section is only executed if Age was less than 17.
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
TRY
Sample program:
# Selection commands
# Main program
ValidMonth(9)
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
# Main program 4. …otherwise this line of code inside the tabulated indent is executed instead.
ValidMonth(9)
START
HERE
1. The program starts here by running the subroutine called ValidMonth, passing the number 9.
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
TRY
Sample program:
# Selection commands
# Main program
YearGroup(10)
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
# Main program
YearGroup(10)
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Learning points from this objective:
• Checking the value of a variable and executing instructions depending on the outcome of the check is known as a selection.
• The instructions that are executed as a result of a selection is known as a program branch.
• The logical checking of a variable against another value is known as a condition.
• A Boolean variable used in condition is also known as a flag.
• Conditions can be enclosed in brackets if you want to combine multiple conditions together into a single expression.
• Code for each program branch must be indented with a tab. This also makes it easier to read where a program branch begins and ends.
• It is good practice to comment a selection to explain its purpose.
• One “if” construct can also be placed inside another “if” construct, this is known as nesting.
• The condition is either true or false. E.g. X = 6 : If X > 8 returns false.
• Logical operators are used in conditions:
• and - And
• or - Or
• not - Not
• == - Equal to
• != - Not equal to
• < - Less than
• <= - Less than or equal to
• > - Greater than
• >= - Greater than or equal to
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Program comprehension: ITEM Identify a selection statement in the program.
1. def Sample(Hz):
2. if Hz == 44100:
3. print("CD quality") Identify a condition in the program.
4. if Hz < 44100:
5. print("Low quality")
6. if Hz > 44100:
7. print("Compress")
Identify a logical operator in the program.
8.
9. Sample(48000)
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Why is == used instead of = in line 2?
Program comprehension: REASON
1. def Sample(Hz):
2. if Hz == 44100:
3. print("CD quality") What advantage would there be to using “elif” instead of “if” in
lines 4 and 6?
4. if Hz < 44100:
5. print("Low quality")
6. if Hz > 44100:
7. print("Compress")
8.
9. Sample(48000)
What will be the affect on the output from the program if lines
RELATION 2-3 are swapped around with lines 4-5?
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
INVESTIGATE
Keywords introduced in this objective:
if X == "Craig" or X == "Dave“: Branches to a sequence of instructions based on the condition.
commands In this situation if X is Craig or X is Dave.
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
MAKE
# Driving test problem
return "pass"
def PassFail(MinorFaults):
# Main program
return "fail"
print(PassFail(16))
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
MAKE
Max problem:
1 point.
Write a function called Max that returns the highest of two number parameters. E.g. Max(50,32) would return 50.
Output the returned value.
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
MAKE
Career quote problem:
2 points.
Write a subroutine that takes a parameter called Job. It outputs a quote as shown in the table below:
Job Quote
Developer Logical thinking, passion and perseverance is the paint on your palette.
Analyst Seeing what other people can’t see gives you great vision.
None of the above I'm sorry. We could not find a quote for your job.
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
Nitrate problem:
2 points.
Yes Is nitrate No
When keeping fish, one of the goals to
reduce algae is to keep nitrates to a above 10?
minimum. One way of doing this is to
dose a carbon source which nitrifying
bacteria within an aquarium consume Yes Is nitrate
together with nitrates. The carbon source
above 2.5?
must be dosed very precisely.
Write this function to determine and
return the dose. No
The program should output:
“For x nitrate dose y ml”
Where x is the parameter and y is the Is nitrate
returned value. Return Return Yes above 1? No
3 2
Return Return
1 0.5
End Function
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
MAKE
Exam grade problem:
3 points.
Write a function that returns the grade and the number of marks to the next grade from an exam result using the data below. The main
program should output a suitable message to the student. E.g. A mark of 59 is grade 7. You needed 8 more marks for the next grade.
Mark <2 2 4 13 22 31 41 54 67 80
Grade U 1 2 3 4 5 6 7 8 9
Element: Lithium
Atomic weight: 6.94
Group: Alkali metals
You only need to use six elements from two different groups.
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
MAKE
Day format problem:
3 points.
Write a function called DayFormat that takes two parameters, the day as a number (e.g. Monday is 1, Tuesday is 2) and the output format
required. The function should return the day formatted as shown:
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
EVALUATE
Test tables:
Problem:
Problem:
PYTHON T I M E
[Header
Learntext]
how to use selection Craig’n’Dave
Craig’n’Dave
EVALUATE
Review your solutions:
Did you: Self-assess: Yes/No
Use a comment after each program branch to explain the purpose of the section?
PYTHON T I M E