liveBook · Manning

Article

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

An indefinite article is a grammatical determiner, such as “a” or “an,” used with a nonspecific noun.

Chooses “a” or “an” before a word by inspecting its first character: vowel starts receive “an”; other starts receive “a.” section 2.0 section 2.1.11

Implement the rule incrementally:

  • Index the word with word[0], normalize it with lower(), and test membership in 'aeiou'. section 2.1.13 section 2.3.3
  • Use an if expression to select both possible outcomes: article = 'an' if word[0].lower() in 'aeiou' else 'a'. section 2.1.11 section 2.3.3
  • Insert the selected article into a formatted output string and verify changes with pytest or make test. section 2.1.13

Command-line output

Running crowsnest.py with narwhal produces “Ahoy, Captain, a narwhal off the larboard bow!”; octopus produces “Ahoy, Captain, an octopus off the larboard bow!” section 2.0

Avast, you corny-faced gollumpus! Ye are barrelman for this watch. D’ye ken what I mean, ye addle-pated blunderbuss?! Ah, landlubber ye be! OK, then, you are the lookout in the crow’s nest--the little bucket attached to the top of a mast of a sailing ship. Your job is to keep a lookout for interesting or dangerous things, like a ship to plunder or an iceberg to avoid. When you see something like a narwhal, you are supposed to cry out, “Ahoy, Captain, a narwhal off the larboard bow!” If you see an octopus, you’ll shout “Ahoy, Captain, an octopus off the larboard bow!” (We’ll assume everything is “off the larboard bow” for this exercise. It’s a great place for things to be.)

Use the lookout scenario to connect the article choice with a concrete spoken alert. section 2.0

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains first-character classification and conditional expressions, but stops short of phonetic exceptions such as silent h or consonant-sounding vowels.
Examples
Demonstrates narwhal, octopus, and galleon, using interactive checks and command-line output.
What it assumes
Uses positional arguments, string indexing, case conversion, membership tests, formatting, and basic conditional logic.
What you can do after
Apply it via Python string operations and conditional expressions to build and test an article-selecting command-line program.
How it connects
Connects strings, indexing, membership testing, conditional expressions, command-line arguments, formatted strings, and testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.