donnemartin/interactive-coding-challenges · error · TypeError
sentence cannot be None
Error message
sentence cannot be None
What it means
This TypeError is raised by the brute-force sentence screen fit solver when the sentence argument is None. It is a deliberate input-validation guard at the top of count_sentence_fit_brute_force, before any algorithm logic runs, because joining/iterating a None sentence would otherwise fail with a less clear AttributeError. It signals the caller passed no sentence data to the API.
Source
Thrown at online_judges/sentence_screen_fit/sentence_screen_fit_solution.ipynb:163
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def count_sentence_fit_brute_force(self, sentence, rows, cols):\n",
" if sentence is None:\n",
" raise TypeError('sentence cannot be None')\n",
" if rows is None or cols is None:\n",
" raise TypeError('rows and cols cannot be None')\n",
" if rows < 0 or cols < 0:\n",
" raise ValueError('rows and cols cannot be negative')\n",
" if cols == 0 or not sentence:\n",
" return 0\n",
" curr_row = 0\n",
" curr_col = 0\n",
" count = 0\n",
" while curr_row < cols:\n",
" for word in sentence:\n",
" # If the current word doesn't fit on the current line,\n",
" # move to the next line\n",
" if len(word) > cols - curr_col:\n",
" curr_col = 0\n",
" curr_row += 1\n",
" # If we are beyond the number of rows, return\n",
" if curr_row >= rows:\n",View on GitHub (pinned to 358f2cc604)
Solutions
- Pass a valid list of sentence strings, e.g. ['hello', 'world']
- Check the upstream variable that produces sentence and give it a default: sentence = sentence or []
- Add a unit test asserting TypeError is raised for None to make the contract explicit
Example fix
// before sol.count_sentence_fit_brute_force(None, 5, 10) // after sol.count_sentence_fit_brute_force(['hello', 'world'], 5, 10)
Defensive patterns
Strategy: validation
Validate before calling
if sentence is None:
raise ValueError('sentence input is required')
# or: sentence = sentence or []
count = sol.count_sentence_fit_brute_force(sentence, rows, cols) Type guard
def is_sentence_list(x):
return isinstance(x, list) and all(isinstance(w, str) for w in x) Try / catch
try:
n = sol.count_sentence_fit_brute_force(sentence, rows, cols)
except TypeError as e:
if 'sentence cannot be None' in str(e):
n = 0 # nothing to fit
else:
raise Prevention
- Initialize sentence collections to [] instead of None
- Assert required inputs before calling solver methods
- Cover the None contract in unit tests
When it happens
Trigger: Calling Solution().count_sentence_fit_brute_force(None, rows, cols) with any rows/cols values; e.g. passing a variable that was initialized to None or a lookup that returned None from a dict of test sentences.
Common situations: Test harnesses iterating over optional inputs where the sentence key is missing; refactoring code so the sentence list is conditionally built and stays None on the empty path; JSON/YAML config where the sentences field was omitted.
Related errors
- rows and cols cannot be None
- prices must have at least two values
- array cannot be None
- array must have 3 or more ints
- rows and cols cannot be negative
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/fadb527a7606bc26.
Report an issue: GitHub.