0% found this document useful (0 votes)
27 views

T3 Worksheet 3

This document contains pseudocode algorithms and tasks related to computational thinking concepts. It includes algorithms for: - Choosing data types for given values - Evaluating Boolean expressions - Printing selected numbers in a range - Counting scores over 100 that a user inputs - Calculating the average of integer scores input by a user - A game of rock paper scissors that plays 5 rounds between two randomly generated hands The document demonstrates pseudocode constructs like sequence, selection, iteration and introduces the concept of a subprogram to improve duplicated code.

Uploaded by

maxvince2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

T3 Worksheet 3

This document contains pseudocode algorithms and tasks related to computational thinking concepts. It includes algorithms for: - Choosing data types for given values - Evaluating Boolean expressions - Printing selected numbers in a range - Counting scores over 100 that a user inputs - Calculating the average of integer scores input by a user - A game of rock paper scissors that plays 5 rounds between two randomly generated hands The document demonstrates pseudocode constructs like sequence, selection, iteration and introduces the concept of a subprogram to improve duplicated code.

Uploaded by

maxvince2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Worksheet 3

Developing algorithms using pseudocode


Unit 1 Computational thinking
Name:...................................................................................................... Class: ......................

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.

Value Data type Value Data type

3 Integer "AB" String

True Boolean 3.02 Float

-6 Integer "45" String

"Hello" String False Boolean

-3.0 Float "P" String

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.

Expression Evaluates to… Expression Evaluates to…

5>2 True 80 < 80 False

24 + 1 <= 25 True 16 – 1 < 16 True

5^2 == 25 True 3 >= 3 True


True
5 != 3 True "Hel" + "lo" == "Hello"
Concatenation
2 == "2" False age == age + 1 False

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

(b) Which lines show an example of the ‘Selection’ programming construct?


Line 8

(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?

1 print("This program prints selected numbers in in given range. ")


2 anotherGo = "Yes"
3 while anotherGo == "Yes":
4 lowNumber = input("Enter low number: ")
5 highNumber = input("Enter high number: ")
6 x = 0
7 for i in range(lowNumber, highNumber+1):
8 if i % 5 != 0 and i % 7 != 0:
9 print(i)
10 x = x + 1
11 print(x,"numbers")
12 anotherGo = input("Enter 'Yes' for another go: ")

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

(b) Write pseudocode to rewrite the above code using a subprogram.

You might also like