Year 11 – Python Functions Tasks
Instructions:
• Answer ALL tasks below.
• Some tasks require you to write a function from scratch.
• Some tasks contain incomplete or incorrect code.
• Make sure your functions are called and tested where required.
Task 1: Simple Function
Write a function called say_hello() that prints:
Hello, World!
Call the function once after it is defined.
Task 2: Function With One Parameter
Write a function called square_number(num) that:
• takes one number as a parameter
• returns the square of the number
Call the function with the value 5 and print the result.
Task 3: Function With Two Parameters
Write a function called add_numbers(a, b) that:
• takes two numbers
• returns their total
Test the function with 10 and 7.
Task 4: Complete the Function
Complete the function so it returns the largest number.
def largest_number(a, b):
# write your code here
Call the function with 8 and 12 and print the result.
Task 5: Missing Return Statement
The function should calculate the average of two numbers.
def average(num1, num2):
total = num1 + num2
# missing return
Call the function using 4 and 6.
Task 6: Debugging a Function
The function should calculate the product of all numbers in a list.
The code contains THREE errors. Fix them.
def multiply_list(numbers)
total = 1
for i in numbers:
total = total + i
return total
print(multiply_list([2, 3, 4]))
Task 7: Boolean Function With Multiple Conditions
Complete the function so it returns True if:
• the number is even
• AND greater than 10
Otherwise, return False.
def valid_number(num):
# write your code here
Test with: 8, 12, 15
Task 8: Function With Input Validation
Complete the function so it:
• returns 'Valid' if the number is between 1 and 100 (inclusive)
• returns 'Invalid' otherwise
def check_range(number):
# write your code here
Then:
• ask the user for a number
• convert it to an integer
• pass it into the function
• print the result
Task 9: Loop + Condition Inside a Function
Complete the function so it counts how many numbers in the list are greater than 5.
def count_greater_than_five(numbers):
count = 0
for num in numbers:
# write your code here
return count
Test using: [3, 6, 9, 2, 7, 1]
Task 10: Challenge Task
Complete the function so it counts how many uppercase letters are in a string.
def count_uppercase(text):
count = 0
for char in text:
# write your code here
return count
Test using: 'Hello World!'