How does a counting loop with exactly two cases turn a page of lyrics into a dictionary of word frequencies?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
The test song is short, repetitive, and written entirely in capitals. Its punctuation was stripped beforehand, because stray commas would corrupt the counts:
song = "RAH RAH AH AH AH ROM MAH RO MAH MAH"
Assign to words_list that song's words as a list, with every letter in lowercase — the ten-element list the counting loop will later walk over. Capitals have to go, so that a capitalised word and a lowercase one count as the same word.
(Hint: one string method returns an all-lowercase copy, and another breaks a string into a list of words when called with nothing inside the parentheses: .lower() and .split().)
Dividing the most-common-words task into three
List the three steps in order, building the frequency dictionary, finding the most frequent word, and collecting every word above a cutoff, and record the shape of the final output list.
Normalizing the song string
Write song.lower() followed by .split() with no argument, and show 'RAH RAH AH AH AH ROM MAH RO MAH MAH' becoming a ten-element list of lowercase words.
The two-case counting loop
Write generate_word_dict with if w in word_dict incrementing and else setting the count to 1, and trace the whole song to {'rah': 2, 'ah': 3, 'rom': 1, 'mah': 3, 'ro': 1}.