menu

Clean code using functions in Python

Using Python, write a script which uses functions and cleanly formatted code to maximise program structure and clarity.

To claim this badge, you may either provide evidence from an existing project OR complete the student task. Both assessment options are detailed below.

As well as providing code evidence, you must also answer a series of questions about your code.

Project-Based Assessment

For this assessment, you can use one or more code files in your existing project as evidence of understanding.

The code must meet the requirements outlined in “Code Requirements” below.

You will need to show the code to your mentor and then answer the questions described under “Assessment Questions” below.

Task-Based Assessment

This task is designed to assess whether you understand how to use functions to divide up code into purposeful pieces.

You may use web search to complete this task, including the Python documentation and StackOverflow. All research and compilation of the code must be your own work.

You will need to show the code to your mentor and then answer the questions described under “Assessment Questions” below.

  1. Right-click and save the script composer-task.py from here.
  2. Refactor the script composer-task.py to use functions.
  3. Ensure the code meets the “Code Requirements” described below.

Code Requirements

All code submitted for this assessment must meet the following requirements:

  1. All function names must start with a verb.
  2. Functions have a line limit of 15 lines (blank lines excluded).
  3. At least one function must demonstrate the use of parameters.
  4. At least one function must demonstrate the use of a return value.
  5. The script usage and output must remain the same.

View Assessor Guide

Assessor Checklist

  1. Script runs from shell
  2. Functions no longer than 15 lines
  3. Code clarity not sacrificed to meet line restrictions
  4. Uses descriptive function names starting with a verb
  5. Demonstrates use of function parameters
  6. Demonstrates use of return values
  7. Executes in same way as original code