Professional Documents
Culture Documents
Y10 C3 CT16 Activities
Y10 C3 CT16 Activities
Functions
Activity 1
Complete the table below to indicate if the instruction in the first column is a function or a
procedure. If it is a function, then give its return value. One has been done for you.
len("Hello World") 11
Function
chr (65) A
Function
ord ("A") 65
Function
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
1
TOPIC 1: COMPUTATIONAL THINKING
Functions
Activity 2
A program simulates the roll of a dice, but the user can determine the number of sides on the
dice. These kinds of dice are called ‘Polyhedra’ dice.
The lines of code below are all jumbled up. Arrange them in order. (One way to tackle this
would be to cut the lines apart and arrange them on the layout on the text page).
if (isRebel (numSides)):
else:
myRoll = polyhedraDice(numSides)
numSides = 0
myRoll = 0
roll = 0
status = True
def isRebel(numSides):
return (roll)
return (status)
import random
print ("You're a dice rebel, but I'll roll that dice for you!!")
print ("You're a bit conservative, but you can still have a roll.")
numSides = int (input ("Enter the number of sides on your die: "))
# ------------------------------------------------------------
# Import libraries
# ------------------------------------------------------------
# ------------------------------------------------------------
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
2
TOPIC 1: COMPUTATIONAL THINKING
Functions
# Global variables
# ------------------------------------------------------------
# ------------------------------------------------------------
# Subprograms
# ------------------------------------------------------------
# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------
import random
def isRebel(numSides):
return (status)
def polyhedraDice(pSides):
roll = random.randint(1, pSides)
return roll
if isRebel(numSides):
print("You're a dice rebel, but I'll roll that dice for you!!")
else:
if (numSides < 4) or (numSides > 8):
print("You're a bit conservative, but you can still have a roll.")
myRoll = polyhedraDice(numSides)
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
3
TOPIC 1: COMPUTATIONAL THINKING
Functions
Activity 3
A program selects a random drink from a list of drinks.
Load ‘Act3_Student’ into your programming environment.
Set breakpoints on line 16, 17, 19, 20, 21, 26 and 27.
Run the code under debug and complete the table.
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
4
TOPIC 1: COMPUTATIONAL THINKING
Functions
Activity 4 (Optional)
Write a program that meets these requirements:
A main program that:
asks for the user’s name
asks for the user’s favourite number
displays a key for the user.
A subprogram that:
takes two parameters
makes a key by joining the first two letters of the user’s name and their favourite
number.
import random
Name = input("Please input your name")
Num = int(input("Please input your favourite number:"))
key = random.randint(4,909)
print (key)
def generator(Name, Num):
Fn = Name[:2]
return(Fn +str(Num))
Final = generator(Name, Num)
print (Final)
© Pearson Education Ltd 2020. Copying permitted for purchasing institution only. This material is not copyright free.
5