4 - Programming Companion Python-If Satements
4 - Programming Companion Python-If Satements
Tasks
1. Try entering the following commands and see what happens:
2. Try entering the following commands and run the program three times with the test data:
6, 51 and 70.
Pay careful attention to the indentation with tabs, these are not spaces.
Note how the program only executes instructions if the condition is true i.e. the score is more than
40, and how it is possible to use an else statement to run an alternative set of commands if the
condition is false.
else:
print("The numbers are the same")
5. Try entering the following commands and run the program three times with the test data:
1, 3, 5.
Note how ‘and’ can be used to check if both conditions are true. You can also use ‘or’ if only one
condition needs to be true and ‘not’ as an alternative to does not equal.
Note how the number was input as a string, not an integer, yet greater than and less than operators
still work to check if the input was between 1 and 2. That’s because the ASCII value of the character is
being checked, not the actual number. This approach is helpful as it prevents the program crashing if a
character is input instead of a number.
6. Try entering the following commands and see what happens:
Note how it is possible to use elif to have multiple branches rather than just true or false.
Objective 4: Key learning points
How to use selection statements
• 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.
• Elif commands can be used instead of multiple if statements.
• Code for each program branch must be indented.
• It is good practice to comment a selection to explain its purpose.
• One ‘If’ can be placed inside another ‘If’, this is known as nesting.
x is the variable being checked. Multiple variables can be checked in one condition. Use brackets to
express order of priority. In the example code, x must be between 0 and 5 or equal to 10 for the condition
to be true. Else is optional and is used to branch if the condition is false. All program branches must be
indented.
elif
Example code: if x == 3:
…
elif x < 3:
…
elif x >= 10 and x <= 20:
…
else:
…
Write a program that asks for your age. If you are over 18 it outputs the message, “Over 18”, otherwise it
outputs, “Under 18”.
Write a program that reads in the temperature of water in a container in Centigrade and displays a
message stating whether the water is frozen (zero or below), boiling (100 or greater) or neither.
Write a program that allows you to enter a test mark out of 100.
The program outputs “FAIL” for a score less than 40, “PASS” for a score of 40 or more, “MERIT” for a score
of 60 or more and “DISTINCTION” for a score of 80 or more.
Write a program that asks for a number and outputs that number as a graphical dice. E.g.
oooooooooooo
o o
o # o
o # o
o # o
o o
oooooooooooo
Greatest number challenge
Difficulty: G
Start
Input number1
from keyboard
Input number2
from keyboard
Is number1
Yes No
greater than
number2?
Stop
Nitrate Challenge
Difficulty: G
When keeping fish, one of the goals to reduce algae is to keep nitrates to a minimum. One way of doing
this is to dose a carbon source which nitrifying bacteria within an aquarium consume together with
nitrates. The carbon source has to be dosed very precisely. Write this program to determine the dose:
Start
Yes Is nitrate No
above 10?
Output
“Dose 3ml” Yes Is nitrate
above 2.5?
No
No
Stop
Portfolio grade challenge
Difficulty: G
Write a program that inputs a mark from the keyboard for sections of a project: ‘analysis’, ‘design’,
‘implementation’ and ‘evaluation’. The program should output the total mark, the grade, and how many
more marks were needed to get into the next mark band.
Grades are:
0 U
4 G
13 F
22 E
31 D
41 C
54 B
67 A
80 A*
A cash machine dispenses £10 and £20 notes to a maximum of £250. Write a program that shows the user
their balance, asks them how much to withdraw, ensures this is a valid amount without going overdrawn
and with the notes available and outputs the new balance.
Write a program that asks the user to enter the symbol or name of an element, or group it belongs to. The
program should output the name of the element and its atomic weight. E.g.
Element: Lithium
Atomic weight: 6.94
Group: Alkali metals
If the user enters Alkali metals, the program outputs the data for all the elements in the alkali metals
group.
You only need to get this working for 6 elements from two different groups.
Train ticket challenge
Difficulty: A
Write a program that allows the user to enter how many stations they need to pass through, how many
adults, how many children and the time of day (as a number: 24 hour clock). The program should output
the correct fare.
Write a program that asks the user for the number of hours worked this week and their hourly rate of pay.
The program is to calculate the gross pay. If the number of hours worked is greater than 40, the extra
hours are paid at 1.5 times the rate. The program should display an error message if the number of hours
worked is not in the range 0 to 60.