Help with Python code for CodeX

Question from a student - posting here to see if maybe @Sam could help?

The student says the sound is not working on his remix :frowning: See code below!

from codex import*
from time import sleep

delay = 1

color = RED
pixels.set(0, color)
sleep(delay)

color = YELLOW
pixels.set(1, color)
sleep(delay)

color = GREEN
pixels.set(2, color)
sleep(delay)

color = BLUE
pixels.set(3, color)
sleep(delay)

while True:
if buttons.is_pressed(BTN_A):
    audio.mp3("sounds/africa")
sleep(0.05)

The pixel sequence looks fine — the sound never gets a chance to run because of indentation.

In the code as posted, Python expects an indented block after while True:, but the if is sitting at the same indent level. That usually shows up as an IndentationError in the CodeSpace debug console, and the button/MP3 loop never starts.

Try this for the last part:

while True:
    if buttons.is_pressed(BTN_A):
        audio.mp3("sounds/africa")
    sleep(0.05)

A couple of extra notes:

  • audio.mp3("sounds/africa") is the right path for the built-in song. The .mp3 extension is optional — see the CodeX audio docs.
  • audio.mp3() plays the whole file before the loop continues, so a press on A should still work once the indent is fixed.
  • Worth adding a space in from codex import * for readability (Python will accept it either way).

If the lights work but there’s still no sound after that change, check the debug console for a file-not-found error, and that volume isn’t turned down / the speaker isn’t muted by headphones.

@Kathy @dewing