It's the code that makes the computer perform different tasks in different situations. Selection is sometimes referred to as a decision.
Whenever we make an IF this THEN that type decision we are using selection logic.
For example, real life decisions could be:
IF it is raining THEN
Take your umbrella.
Or:
IF you haven’t done your homework THEN
You get grounded.
In both of these examples, the instruction on the second line is only followed if the scenario on the first line is true. Otherwise it is skipped over. This is an essential part of selection.
Whereas in all of the code we have written before the computer runs each line one at a time, selection relies on a Boolean Condition to decide whether or not to run the code.
Boolean conditions are true or false checks. If a condition is checked and comes back as true, the code on the line(s) below runs. If it comes back as false, then the computer skips the commands and moves on with the rest of the program.
Let’s put that into practice with the umbrella example above:
IF it is raining THEN
Take your umbrella
The condition is in bold. It is either raining (True) or not (False). If it is raining (true) then the instruction to take your umbrella is carried out. If not (false) then we would skip down to the next instruction that wasn’t part of the selection (probably something like ‘leave the house’).
Boolean conditions are written using logical operators. These are:
== Equal to/Same as (a single = is used to store data in a variable, so Python uses double = to compare two pieces of data)
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Let’s use the umbrella example again. We want to compare the weather to rain to see if they are the same, so we would write our condition like this:
IF weather == “rain” THEN
Take your umbrella
Case matters when comparing text using ==. The match has to be exact. For the example above, if the user inputs ‘rain’ then the condition will be true. If they input ‘Rain’ then it will be false.
Pseudocode syntax:
IF condition
THEN
Code to run if the condition above is true.
ENDIF
Translate the following pseudocode to Python and add comments to explain what the input line does, and when the program below will output the text 'This text is output because the condition was true'
INPUT num1
IF num1 = 10
THEN
OUTPUT "This text is output because the condition was true"
ENDIF
Edit the program below so that it works properly for the correct legal driving age. Add comments to explain what the Boolean operator means.
INPUT age
IF age > 20
THEN
OUTPUT "You are old enough to drive"
ENDIF
These selection examples output one of two different statements. The indented code below the ‘if’ runs when the condition is true. The ‘else’ is used as a catch all - if the condition is false then the computer skips down to the else and runs the indented code there instead.
Pseudocode syntax:
IF condition
THEN
Code to run if condition above is true.
ELSE
Code to run if condition is false.
ENDIF
Translate the following pseudocode to Python code and add comments to the code to explain:
What will be the output when the code is run?
In what circumstances would the other output message be produced
INPUT num1
IF num1 == 42
THEN
OUTPUT "You have discovered the meaning of life!"
ELSE
OUTPUT "Sorry, you have failed to discover the meaning of life!"
ENDIF
Translate the following pseudocode to Python code and add to the code below so that it outputs 'You're not Odaly!' if the user does not input 'Odaly'
INPUT name
IF name == "Odaly"
THEN
OUTPUT "Hello Odaly"
ENDIF
Write a program that:
Stores the number 1337 in a variable called 'password'
Asks the user to guess the password and stores their input in a new variable (you choose the name of this new variable)
If the user inputs 1337 then output 'Password correct', otherwise output 'Password incorrect'
Write an algorithm that:
Allows player 1 to enter his score
Allows player 2 to enter his score
Your program should find out who is the winner, player 1 or player 2?
Pseudocode syntax:
IF condition1
THEN
Code to run if condition1 is true.
ELSE
IF condition2
THEN
Code to run if condition2 is true.
ELSE
IF condition3
THEN
Code to run if condition3 is true.
ELSE
... as many IFs as you need or no more IFs...
ENDIF
ENDIF
ENDIF
Translate the following pseudocode to Python code and add comments to explain the overall job of the program.
INPUT num1
INPUT num2
IF num1 > num2
THEN
OUTPUT num1 & "is bigger"
ELSE
IF num1 < num2
THEN
OUTPUT num2 & " is bigger"
ELSE
OUTPUT "The numbers are the same"
ENDIF
ENDIF
QUESTION - Why do we not need a condition after the 'else'?
Add to the program so that if the user inputs 'Music' they get a message saying 'Not bad, but Computing is better'. For all other subjects the user should get a message saying 'How wrong can you be? Computing is way better than that!'
INPUT subject
IF subject = "Computing"
THEN
OUTPUT "That is the right answer!"
ELSE
IF subject = "computing":
OUTPUT "Correct! But Computing starts with a capital 'C' "
ENDIF
ENDIF
A shop sales all their products for 15€.
The shop gives a discount of 10% if the total cost is more than 1000€.
Write a program that:
Asks the user for the quantity of items they want to buy.
Checks if the user got a discount or not.
If the user got a discount then calculates the total cost to pay and outputs "Congratulations! You got a 10% discount. The total to pay is: " and shows the total cost taking into account the discount.
If the user did not get a discount then your program outputs "You do not get any discount. The total to pay is: " and shows the total cost.
The Sports Show Center would like to output a brief description of each sport in the list below every time an interested client enters the name of the sport on their computer system.
The list of sports they offer and their descriptions is:
Football - Kick a ball in a goal.
Basketball - Throw a ball in a basket.
Tennis - Hit a ball with a racket.
Write an algorithm in pseudocode to:
allow you to enter a sport from the list above
output its corresponding description
For example, if the user enters Basketball your algorithm should output "Throw a ball in a basket."
Horses are entered for a horse race. A horse may have to carry a penalty weight in addition to the rider. This weight is added to the saddle. The penalty weight (if any) depends on the number of wins the horse has achieved in previous races.
The penalty weight is calculated as follows:
Number of previous wins Penalty weight (kg)
0 0
1 or 2 4
over 2 8
A program is to be written from the following structured English design.
1 INPUT name of horse
2 INPUT number of previous wins
3 CALCULATE penalty weight
4 STORE penalty weight
5 OUTPUT name of horse, penalty weight
Write a program that outputs the letter that the user enters. For example:
Enter a letter: A
your program will print
This is a snippet of the program in Python:
you have to replace the last line "print........." with your own code. And then continue with C, D, E, F, G. If you have time, carry on until Z.
Write an algorithm in pseudocode and Python that allows the user to enter his age and returns whether the user is a senior, an adult, a teenager, a child or an infant.
age > 65, senior
age > 18, adult
age > 12, teenager
age > 3, child
otherwise infant.
Write an algorithm in pseudocode and Python code that allows the user to enter the day, the month and the year in three separate variables.
Your algorithm should check whether that date belongs to the 20th century or not.
For example, if you enter:
25 for the day
11 for the month
2005 for the year
your algorithm should print: "That date does not belong to the 20th century."
But, if you enter:
40 for the day
10 for the month
1999 for the year
your algorithm should print: "That date is not valid, the day cannot be more than 31."
Task 15
A programmer wants to write a program to calculate the baggage charge for a passenger flight.
Two types of ticket are available for a flight:
economy class (coded E)
standard class (coded S)
Each ticket type has a baggage weight allowance as shown below. The airline makes a charge if the weight exceeds the allowance.
Write an algorithm in pseudocode and Python that:
will allow the user to input the code (E or S) for the ticket type
will allow the user to input the baggage weight
will calculate the charge rate:
if the baggage weight is more than 16kg and the ticket type is E then charge 3.50$us
if the baggage weight is bigger than 20kg and the ticket type is S then charge 5.75$us
otherwise do not charge anything
outputs an appropriate message
Task 16
Write an algorithm that:
allows you to input a character from the English alphabet
checks whether the character is a vowel or not
outputs appropriate messages. For example, if the character is a vowel outputs:
The character u is a vowel
if the character is not a vowel outputs:
The character b is not a vowel