← Back to course
Code Crew G4 / Week 06 / Friday
5/6
Week 06 Β· Split and join

Friday

The hidden message
// Decode the comms log
⏱ about 15 min

Friday: The hidden message

A strange line appears in the comms log: lights are now dim. "That is odd," Rocket says. "The lights are fine."

Raven writes the four words in a column. "What do you notice about the first letter of each word?" she asks.

Rocket reads them down the side. "L, A, N, D. Land! It is a secret message from the shuttle!" Nova does a happy loop. "Great discovery! Would you like a hint? A loop could find the first letters for us."

Split, loop, join

This program splits the log line into words.

The loop appends the first letter of each word to a list.

Then join glues the letters together, and upper makes them capitals.

log = "lights are now dim"
letters = []
for w in log.split():
    letters.append(w[0])
print("".join(letters).upper())
The screen shows
LAND
Roundwletters after the round
1lights['l']
2are['l', 'a']
3now['l', 'a', 'n']
4dim['l', 'a', 'n', 'd']
Change the log to "watch all its tanks". What does the program show?
WHY THIS EXERCISEThe program works for any log line. That is the power of a loop.

Debug the decoder

Rocket made one small change. Now the decoder shows only L.

log = "lights are now dim"
letters = []
for w in log.split(","):
    letters.append(w[0])
print("".join(letters).upper())
FIND THE BUG
  • Read the question.
  • Tap your answer.
Why does the decoder show only L?
Which fix makes it show LAND again?
Change to the working decoderWould the screen still show LAND?
Change "".join to "-".join?
Take out upper()?
Rename letters to marks on every line?
WHY THIS EXERCISEPredicting how a change affects output is a big debugging skill.
At the computer
This is never required.
If a grown-up has set up Python from python.org, type the decoder into IDLE.
Write your own secret log line and decode it.

Practice lab: the last-letter code

log = "bus hat sofa car"
letters = []
for w in log.split():
    letters.append(w[-1])
print("".join(letters).upper())
What word does the program show?
How many times does the loop run?
How many items are in letters at the end?
DECODER CHECK
  • Read the question.
  • Tap your answer.
What does w[-1] grab?
In round 3, which letter is appended?
Suppose the log is "rim two saw". What would the program show?
Change to the last-letter decoderWould the screen still show STAR?
Change w[-1] to w[0]?
Change "".join to " ".join?
Change the log to "plus melt tuna door"?
Take out .upper()?
WHY THIS EXERCISEEach part of the decoder has a job. Change one and the output changes.
Use first letters this time. What word hides in "sun kites yellow"?
WHY THIS EXERCISEs, k and y spell sky.

Week six is done, Crew Engineer! You can split, join and decode. Next week, a new kind of container will make the cargo inventory easy.

← Thursday