T3 Worksheet 3
T3 Worksheet 3
Task 1
The below table shows a number of values. For each one, choose the correct data type used to
store it. The first row has been completed for you.
Task 2
The below table shows a number of Boolean expressions. For each one, state whether it
evaluates to True or False. The first row has been completed for you.
1
Worksheet 3
Developing algorithms using pseudocode
Unit 1 Computational thinking
Task 3
1. Examine the pseudocode program given below. The operator modulus, e.g. a % b gives the
remainder when integer a is divided by integer b.
(a) Which lines show an example of the ‘Sequence’ programming construct?
line 1-2
(c) There are two examples of iteration statements in the program. On which lines do each
of the ‘Iteration’ programming constructs begin and end?
Line 3, line 7
(d) If the user enters 1 and 10 for the lowNumber and highNumber in the range, what will
be printed out at line 11? Which numbers is the program counting?
2
Worksheet 3
Developing algorithms using pseudocode
Unit 1 Computational thinking
2. Write a pseudocode algorithm which allows a user to input numeric scores and prints how
many of them are over 100. The program allows the user to enter any number of scores. If
the user enters “-1” the program will end.
Over100 = 0
Num = 0
While num != -1:
Num = int(input(“Please input scores:”))
If num > 100:
Over100 = Over100 + 1
Print(over100,”of them are over 100”)
3. Write a pseudocode algorithm which inputs integer scores and prints the average score.
The end of the data is signalled by a user input of -1.
Score = 0
Total = 0
Scorenum = 0
While score != -1:
Score = int(input(“Please input scores:”))
Total = total + score
Scorenum = scorenum
Average = total / scorenum
Print(average)
3
Worksheet 3
Developing algorithms using pseudocode
Unit 1 Computational thinking
4
Worksheet 3
Developing algorithms using pseudocode
Unit 1 Computational thinking
4. Nazim wants to create a game of rock, paper and scissors. In this game two people
simultaneously make their hand into a shape representing rock, paper or scissors. Scissors
cuts paper, so wins. Paper wraps rock so wins. Stone breaks scissors, so wins. If both
players choose the same item, it is a draw.
Nazim starts by generating 2 random numbers between 1 and 3 to represent the three
possibilities, and assigning them to rock (1), paper (2) or scissors (3).
(a) Add a selection statement to print out what each player has chosen (rock, paper or
scissors) and “Player1 wins”, “Player 2 wins” or “Draw” as appropriate.
(b) Add statements so that five rounds of the game are played before the program ends.
import random
hand1 = random.randint(1,3)
hand2 = random.randint(1,3)
player1 = ""
player2 = ""
if hand1 == 1:
player1 = "Scissors"
elif hand1 == 2:
player1 = "Paper"
elif hand1 == 3:
player1 = "Stone"
if hand2 == 1:
player2 = "Scissors"
elif hand2 == 2:
player2 = "Paper"
elif hand2 == 3:
player2 = "Stone"
print("Player 1: ", player1, ", Player2: ", player2)
if player1 == player2:
a)
print(“draw”)
if player1 == ‘Paper’ and player2 == ‘Stone’:
print(“Player1 wins”)
else:
print(“Player2 wins”)
if player1 == ‘Stone’ and player2 == ‘Scissors’:
print(“Player1 wins”)
else:
print(“Player2 wins”)
if player1 == ‘Scissors’ and player2 == ‘Paper’:
5
Worksheet 3
Developing algorithms using pseudocode
Unit 1 Computational thinking
print(“Player1 wins”)
else:
print(“Player2 wins”)
b)
round = 0
while round != 5:
round = round + 1
input()
6
Worksheet 3
Developing algorithms using pseudocode
Unit 1 Computational thinking
Extension
In the ‘rock paper scissors program’ the following two sections of code seem very similar to
each other.
if hand1 == 1:
player1 = "Scissors"
elif hand1 == 2:
player1 = "Paper"
elif hand1 == 3:
player1 = "Stone"
if hand2 == 1:
player2 = "Scissors"
elif hand2 == 2:
player2 = "Paper"
elif hand2 == 3:
player2 = "Stone"
(a) Describe how abstraction and a subprogram could improve this code.
7
Worksheet 3
Developing algorithms using pseudocode
Unit 1 Computational thinking