Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

TOPIC 1: COMPUTATIONAL THINKING

Functions

ACTIVITIES FOR Y10-03-CT16

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.

Code Procedure Return value


or function

input ("Enter Y or N ") What the user


Function
types, as a string.

len("Hello World") 11
Function

print("Welcome to my program") Procedure

int (input ("Enter your age in years


Function Age as a int
"))

chr (65) A
Function

ord ("A") 65
Function

list (range(5)) Procedure

myList.append ("Fred") Procedure

myList.append ("Wilma") Procedure

del myList[0] Procedure

random.randint(1, 6) Function A random number

© 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:

if ((numSides < 4) or (numSides > 8)):

myRoll = polyhedraDice(numSides)

numSides = 0

myRoll = 0

roll = 0

status = True

status = False # Never a rebel

def polyhedraDice (pSides):

def isRebel(numSides):

roll = random.randint (1, pSides)

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.")

print ("Sides = " + str(numSides) + " Roll = " + str(myRoll))

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

numSides = int(input("Enter the number of sides on your die: "))

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)

print("Sides =", str(numSides), "Roll =", str(myRoll))

© 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.

Breakpoint Question Answer

26 What is the value of myDrink? Mydrink = “ ”


What is the value of drinks? Drinks = drinks = ["Tea", "Coffee",
"Squash", "Cola"]

16 What is the value of pDrinks? pDrinks = null


No the subprogram that it is in hasn’t
Is the variable ‘drinkNum’ in the
been called yet
inspection window? Why? Why
not?

19 What is the value of ‘drinkNum’? 0

20 What is the value of ‘aDrink’? aDrink = “Water”


Are you currently executing a line inside
inside or outside the subprogram
‘getDrink()'

21 What is the value of ‘aDrink’? “Water”

27 What is the value of ‘myDrink’? myDrink= “”


What value does it match from Pdrinks
inside the subprogram? It is a parameter
Why does it match?

© 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

You might also like