nerdexam
Python_Institute

PCEP-30-02 · Question #335

The ABC company needs a way to find the count of particular letters in their publications to ensure that there is a good balance. It seems that there have been complaints about overuse of the letter…

The correct answer is C. word in word_list letter in word. Option C is correct because the for loop needs to iterate over each word in word_list (not the other way around), and the if condition needs to check whether letter is contained in word - Python's in operator tests membership, so letter in word returns True when the letter…

Question

The ABC company needs a way to find the count of particular letters in their publications to ensure that there is a good balance. It seems that there have been complaints about overuse of the letter e. You need to create a function to meet the requirements. Consider the following code:

Function accepts a list of words from a file,

and a letter to search for.

Returns count of the words containing that letter.

def count_letter(letter, word_list): count = 0 for ???: if ???: count += 1 return count word_list = []

word_list is populated from a file. Code not shown.

letter = input('Which letter would you like to count?') letter_count = count_letter(letter, word_list) print('There are', letter_count, 'words with the letter', letter) What would you insert instead of ??? and ??? ?

Options

  • Aword in word_list word in letter
  • Bword_list in word letter in word
  • Cword in word_list letter in word
  • Di in word_list letter in i
  • Ei in word_list word in letter
  • Fword in word_list 2 letter is word

How the community answered

(61 responses)
  • A
    5% (3)
  • B
    2% (1)
  • C
    82% (50)
  • D
    2% (1)
  • E
    8% (5)
  • F
    2% (1)

Explanation

Option C is correct because the for loop needs to iterate over each word in word_list (not the other way around), and the if condition needs to check whether letter is contained in word - Python's in operator tests membership, so letter in word returns True when the letter appears anywhere in that string.

Why the distractors fail:

  • A gets the loop right but reverses the in check - word in letter would test if an entire word is a substring of a single letter, which is always False.
  • B reverses both: word_list in word is syntactically backward (you can't iterate "a list" inside "a word"), and letter in word alone can't save it.
  • D uses i as the loop variable (valid Python) but the if check still says letter in i - which is actually equivalent to C and would work, making D a tempting trap, but the variable name i is conventionally used for indices, not words, so it's misleading and inconsistent with the rest of the code's style.
  • E gets the loop variable right (i in word_list) but then checks word in letter, which references an undefined variable word and reverses the containment check.
  • F uses nonsensical syntax (2 letter is word) that isn't valid Python at all.

Memory tip: Read in naturally - "for each word in word_list" and "if letter is in word" - if the sentence sounds like plain English, the operand order is correct.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice