Walkthrough: Count Vowels
例题: 数元音字母
The textbook's canonical counter pattern (TP §8.7), applied to vowels instead of a single letter.
Python · runnable
The three-piece pattern
- Initialize —
count = 0 - Traverse + condition —
for char in text: if char in vowels: - Update + return —
count += 1thenreturn count
The in operator on strings
Python · runnable