Skip to main content
ALPHA    This is new software undergoing tests! Thank you for your patience.

Python ~ grade 2

Summary

Prerequisites

Grade 2 is suitable for those with little experience of programming, or those who want to move beyond the deliberately constrained requirements for grade 1. Grades 3, 4 and 5 cover all the important and essential aspects of Python programming at an introductory level, and grade 2 ensures candidates have the skills and knowledge needed to take these next steps with confidence.

We strongly recommend the candidate uses or is familiar with the Mu Python code editor for their project. This grade (and subsequent grades) do not accept projects presented with a block based programming environment.

Core concepts

Candidates are expected to demonstrate knowledge and, where appropriate, application of the following core concepts:

Learning

CodeGrades does not provide teaching or learning content. There are already excellent lessons, resources and educational materials found elsewhere (many for free, via the internet). A good first step for candidates is to type "Python", and the core concept under study, into their preferred search engine.

An important skill to cultivate when learning to code is an ability to find, evaluate and absorb new information, skills and practices. An exciting part of such a fast changing technical world is that there's always something new to learn. Coding is an opportunity for such personal and technical growth.

Our aim is to help candidates become autonomous learners, capable of engaging with, reflecting upon and evaluating the different approaches and techniques they'll encounter as a coder.

While they are responsible for their growth and learning, candidates are supported by CodeGrades through interactions about their project with a mentor. The candidate's project is a way to practise, integrate and make use of the newly discovered skills and knowledge (informed by the core concepts for this and previous grades).

However, candidates may find our guidance for teachers a source of useful information. I explains the sorts of activities and processes to engage in (but not the content) to increase a candidate's chance of fulfilling the requirements of this grade.

The project

This could be the first grade taken, so it’s important to note that code mentors expect the projects to be simple, small and relatively naive in implementation whilst still demonstrating the core requirements listed above.

Since grading is cumulative, candidates should also understand, and if required, demonstrate and describe the core concepts for grade 1.

When candidates have a working project we encourage them to submit it for assessment as soon as possible, so they benefit from the help, support and feedback of a mentor at the earliest possible opportunity.

What does a successful project look like? TODO: Add examples

Assessment

It's helpful to understand how mentors assess and engage with a candidate's project. Our guidance for mentors will help reveal this process from the mentor's point of view.

In the specific case of the approach taken to grade 2, consider the following real-world project.

The creator wanted to make a magic 8 ball fortune telling device from a BBC micro:bit and MicroPython. As well as the source code for the project, they included a README.txt file containing details and instructions:

Super Magic 8 Ball

1. Flash your device with the source code for this project.
2. Optionally attach a speaker to your micro:bit via pin 0 and GND.
3. Attach a power supply (via batteries or USB cable).
4. A magic "8" will fade into the screen to show it's connected to the spirit realm.
5. Ask your yes/no question out loud then shake the device.
6. It will commune with the spirits for a few seconds.
7. The yes/no/don't know response to your question will scroll across the screen.
8. Repeat from step 4 for another reading, or press button A to stop.

Developer setup

* Open Mu.
* Select micro:bit mode.
* Copy in the code.
* Attach a speaker via alligator clips.
* Plug in your device.
* Click the "Flash" button.

The code has plenty of comments to explain what each of the functions do. The "answers"
dictionary contains the prognostications from the spirit world categorised by positive, 
questionable or negative answers. The type of answer defines the sort of emote and tune to
use, as well as the list of candidate answers to the user. At the bottom is the loop used to
handle user interactions.

Other assets such as images and an embedded YouTube video were also provided.

How to plug in the speaker:

An example of an answer to the user:

A short video of the device in use:

The final version of the source code was found in the attached magic8.py file:

"""
Magic 8 ball fortune teller.

Ask a question, shake the device, read the answer divined from the beyond.
"""
from microbit import display, sleep, accelerometer, Image, button_a
import music
import random

answers = {
    "+": {  # Positive answers.
        "emote": Image.HAPPY,
        "tune": music.POWER_UP,
        "answers": [
            "It is certain",
            "It is decidedly so",
            "Without a doubt",
            "Yes, definitely",
            "You may rely on it",
            "As I see it, yes",
            "Most likely",
            "Outlook good",
            "Yes",
            "Signs point to yes",
        ],
    },
    "?": {  # Questionable answers.
        "emote": Image.MEH,
        "tune": music.POWER_DOWN,
        "answers": [
            "Reply hazy, try again",
            "Ask again later",
            "Better not tell you now",
            "Cannot predict now",
            "Concentrate and ask again",
        ],
    },
    "-": {  # Negative answers.
        "emote": Image.SAD,
        "tune": music.WAWAWAWAA,
        "answers": [
            "Don't count on it",
            "My reply is no",
            "My sources say no",
            "Outlook not so good",
            "Very doubtful",
            "No",
            "Signs point to no",
        ],
    },
}


def fade_out(pause=80):
    """
    Fades out the content of the display. Will wait "pause" milliseconds between
    each step of the fade.
    """
    for brightness in range(10):
        sleep(pause)
        for x in range(5):
            for y in range(5):
                val = display.get_pixel(x, y)
                if val > 0:
                    display.set_pixel(x, y, val - 1)


def fade_in(image, pause=80):
    """
    Fades in the display to the passed in image/character.

    If a single character is passed in, it's automatically converted to an Image.

    Will wait "pause" milliseconds between each step of the fade.
    """
    if isinstance(image, str):
        if len(image) == 1:
            image = Image(image)
    for brightness in range(10):
        sleep(pause)
        for x in range(5):
            for y in range(5):
                if image.get_pixel(x, y) > 0:
                    display.set_pixel(x, y, brightness)

def thinking(pause=150):
    """
    Displays a series of images to indicate the device is "thinking". The series of
    images are displayed four times in quick succession with random boops played
    by the device.
    """
    images = [
        Image("?"),
        Image("!"),
        Image("?"),
        Image.YES,
        Image("?"),
        Image.NO,
    ]
    for i in range(4):
        for image in images:
            display.show(image)
            frequency = random.randint(60, 880)
            music.pitch(frequency, pause // 2)
            sleep(pause // 2)
    display.clear()


# Start with the "8" logo.
fade_in("8")
while True:
    # Ask a question and shake the device.
    if accelerometer.was_gesture("shake"):
        # Think while working out the answer, display the emote and play the tune,
        # then scroll the resulting message.
        fade_out()
        thinking()
        outlook = answers[random.choice("+?-")]
        emote = outlook["emote"]
        answer = random.choice(outlook["answers"])
        fade_in(emote, pause=40)
        music.play(outlook["tune"])
        fade_out(pause=40)
        display.scroll(answer, delay=80)
        # Fade back to the default "8" logo (to show the device is waiting).
        fade_in("8")
    # Press button_a to stop.
    elif button_a.was_pressed():
        fade_out()
        sleep(500)
        display.scroll("Bye", delay=80)
        break

When thinking about this project's code, take note that:

A mentor may, at their discretion, ask about core concepts not directly in evidence through a candidate's project. For example, handling return values from a function is clearly missing from the example project and so the creator may be asked to explain this missing aspect to the mentor.

A mentor may, at their discretion, ask about core concepts not directly in evidence through a candidate's project. They will definitely engage with the project via a discussion with the candidate on the project's page.

Evidence of the candidate's engagement, skill and level of attainment is gathered in the ensuing conversation about the project. This is an opportunity to create new and stimulating learning opportunities: a vehicle for the mentor to supportively encourage the candidate to achieve more than they, at first, may have imagined possible.

When the mentor feels they have enough evidence to understand the candidate's level of attainment they write up their feedback and the final result is delivered to the candidate.