liveBook · Manning

Consonant

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

A consonant is a speech sound produced with constriction or closure in the vocal tract and represented by a non-vowel letter in this exercise.

A consonant is treated here as a non-vowel letter, such as b or n, identified by examining a word’s characters. The exercise uses the vowels a, e, i, o, and u as the comparison set; every other checked letter counts as a consonant. section 2.1.10 section 2.1.7

Classify consonants in Python by following a small sequence of string operations:

  • Extract the first character with word[0], remembering that string indexes begin at zero. section 2.1.7
  • Normalize case with lower() or upper() before comparing letters. section 2.1.10
  • Test membership with char in 'aeiou'; a false result identifies the character as a consonant for this exercise. section 2.1.10 section 2.1.13
  • For larger tasks, separate leading consonants from the remainder of a word and generate the consonant collection with a guarded comprehension or filter(). section 14.3.1

Choosing the article

A command-line program checks the first letter of narwhal and prints “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 consonant detection with choosing a or an in a complete output sentence. section 2.0

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

How deep it goes
Implements classification through indexing, case conversion, membership tests, comprehensions, filter(), and regex-based leading-consonant splitting.
Examples
Demonstrates narwhal, octopus, cake, chair, apple, uppercase GUITAR, and consonant-only RDNZL.
What you can do after
Build it from scratch by classifying initial letters, selecting articles, testing changes, and separating leading consonants for rhyme generation.
How it connects
Connects consonants with strings, indexing, case conversion, membership testing, regular expressions, list comprehensions, and filter().

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.