🐸

The Gate Markers

The Data Tinkererpython-data-tinkerer-06-the-gate-markers
Reward: 95 XP
|

The Gate Markers

In the last lesson, we found the position of a clue word in the echo message. Now Hoppy reaches the crystal arch, where a marker is carved along the gate frame: "gate: crystal arch".

This time, Hoppy does not need to search through the middle again or rewrite the text. The gate sprite only checks the edge signals: does it start with gate:? Does it end with arch? When the answer sits on the edge, checking the beginning and ending is the simplest move.

First, see what edge checks give you

The feeling to build here is: sometimes you do not need a full search. You only want to know whether the string carries a signal at one of its edges. This time, the screen does not give you a position number. It gives you True or False.

gate_marker = "gate: moon arch"

print("Marker:", gate_marker)

has_gate_prefix = gate_marker.startswith("gate:")
has_arch_suffix = gate_marker.endswith("arch")

print("Starts with gate:", has_gate_prefix)
print("Ends with arch:", has_arch_suffix)

When you run it, you will see two True values. That means this marker really does carry the beginning signal you care about, and it also carries the ending signal you care about. You are not changing the string, and you are not locating a word in the middle. You are reading its edges.

One kind of move: check the beginning, check the ending

startswith() and endswith() make more sense together than apart here. They are the same kind of move: checking whether the edge of a string carries a signal.

When you care about the beginning, use startswith(). When you care about the ending, use endswith(). If the signal already lives on the edge, you do not need to search the whole string or rewrite the text first.

1
Find the two boolean lines

The starter already shows the original string and the output lines. The real work you need to add is on the two lines for has_gate_prefix and has_arch_suffix.

2
Check the beginning and the ending

Use gate_marker.startswith("gate:") for the prefix check, and gate_marker.endswith("arch") for the suffix check.

3
Run it and notice the two True results

First look at the original marker, then the two boolean results. The instinct to keep is: if the signal I care about is on the edge, checking the edge is enough.

Why is this not a position number this time?

Because you are not asking “Where does it appear?” You are asking “Does it start this way?” or “Does it end this way?” So the result is a boolean value: True or False.

Suggested Solution
Expand
Solution:
gate_marker = "gate: crystal arch"

print("Marker:", gate_marker)

has_gate_prefix = gate_marker.startswith("gate:")
has_arch_suffix = gate_marker.endswith("arch")

print("Starts with gate:", has_gate_prefix)
print("Ends with arch:", has_arch_suffix)
Advanced Tips
Want more? Click to expand

Programmers would say that startswith() and endswith() return boolean values. You only need the names lightly for now. The more important shift is the judgment: when the clue is sitting on the edge of the string, you can check the edge directly.

Later, when you see a piece of text and only care whether it carries a certain beginning or ending signal, you can ask yourself: do I even need a full search, or do I just need to inspect the edges? For now, just get comfortable with this edge-check move.

Loading...
Terminal
Terminal
Ready to run...