CL9 CH5 Functions in Python AS
CL9 CH5 Functions in Python AS
SESSION 5
Functions in Python
In this activity, you will learn the implementation of functions. Here, you will create two functions that
takes the length of a side as an input and returns the surface area and volume of a cube respectively.
MATERIALS REǪUIRED
□ Computer/Laptop/Tab with PictoBlox installed
STEP-BY-STEP
Follow the steps below:
1. Open a New file in PictoBlox:
1.1. Click on PictoBlox icon on your device and
1.2. Select the coding environment as Python Coding
2. The sprite object for Tobi will already be initiated.
sprite = Sprite('Tobi')
3. Create a function called surfaceAreaCube that takes length (of a side) as parameter, calculates the
surface area of a cube and returns the calculated value.
def surfaceAreaCube(length):
# Calculates the surface area of the cube
surfaceArea = 6*length*length
return surfaceArea
4. Similarly, we create a function called volumeCube that takes length (of a side) as parameter, calculates
the volume of a cube and returns the calculated value.
# Calculates the volume of the cube
def volumeCube(length):
volume = length*length*length
return volume
5. Now, we write a code that takes input from a user for length of a side using the input() function from
Sprite class. We can then fetch the input that the user enters using the answer() function from Sprite
class and assign it to the variable “l”.
sprite.input("Enter the side length")
l = int(sprite.answer())
Also, the input fetched will be a string type which we will need to convert to integer type using the in-
built function int() of python.
6. Now, we make the sprite say the Surface area as well as volume of the cube for 2 seconds each.
6.1. We use the str() in-built function of python to convert the numerical values returned by
surfaceAreaCube() and volumeCube() functions to string type.
6.2. Once, we have done this, we can concatenate (combine) the value returned by the function with the
rest of the string using the ‘+’ operator. This step is necessary since we cannot concatenate a string
type to an integer type. Both the values must be of string type in order to be concatenated together.
sprite = Sprite('Tobi')
def surfaceAreaCube(length):
# Calculates the surface area of the cube
surfaceArea = 6*length*length
return surfaceArea
def volumeCube(length):
# Calculates the volume of the cube
volume = length*length*length
return volume
sprite.input("Enter the side length")
l = int(sprite.answer())
STEP-BY-STEP PROCEDURE
Follow the steps below:
1. Open PictoBlox and create a new file from the menu-bar & select the coding environment as Python
Coding.
3. Then we define a function named calarea that takes two parameters: base and height. This function
calculates the area of a right-angled triangle using the formula 0.5*base*height and returns the result.
4. Further we define a function named hyp that takes two parameters: base and height. This function
calculates the hypotenuse of a right-angled triangle using the Pythagorean theorem, which states that the
square of the hypotenuse is equal to the sum of the squares of the other two sides.
5. Then we calculate the hypotenuse using the Pythagorean theorem. It first multiplies the values of base
and height by themselves using the * symbol, adds the two resulting values together using the + symbol,
and then takes the square root of the sum using the math.sqrt() function. The result is stored in the
variable var.
6. Then return is write to specifies the value that the hyp function will return. It returns the value of the var
variable, which represents the hypotenuse of the triangle.
7. Further the user to enter a value for the length of the base of the triangle using the input() function,
which takes the user's input as
a string. The int() function is import math
used to convert the user's input #function to calculate area
from a string to an integer data
type, and the resulting value is def calarea(base,height):
assigned to the variable base.
return(0.5*base*height)
8. Then the user enters a value for
the height of the triangle using
the input() function, which #function to calculate hypotenuse
takes the user's input as a
def hyp(base,height):
string. The int() function is used
to convert the user's input from var=math.sqrt((base*base)+(height*height))
a string to an integer data type,
and the resulting value is return(var)
assigned to the variable height.
9. Then we call the calarea #taking user inputs
function with the values of base
and height passed as base=int(input("Enter the length of the base of the RA
arguments, and assigns the triangle(cm): "))
result to the variable area.
height=int(input("Enter the height of the RA triangle(cm): "))
10. Then we use print statement to
print area and hypotenuse of
tringle. #calling the functions
hypotenuse=hyp(base,height)