mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
added example of human-readable message in assert statement
This commit is contained in:
+13
-10
@@ -51,7 +51,7 @@ import itertools
|
||||
def solve(puzzle):
|
||||
words = re.findall('[A-Z]+', puzzle.upper())
|
||||
unique_characters = set(join(words))
|
||||
assert len(unique_characters) <= 10
|
||||
assert len(unique_characters) <= 10, 'Too many letters'
|
||||
first_letters = {word[0] for word in words}
|
||||
n = len(first_letters)
|
||||
sorted_characters = ''.join(first_letters) + \
|
||||
@@ -161,28 +161,31 @@ if __name__ == '__main__':
|
||||
<p>Like many programming languages, Python has an <code>assert</code> statement. Here’s how it works.
|
||||
|
||||
<pre class=screen>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>assert 1 + 1 == 2</kbd> <span class=u>①</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>assert 1 + 1 == 3</kbd> <span class=u>②</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>assert 1 + 1 == 2</kbd> <span class=u>①</span></a>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>assert 1 + 1 == 3</kbd> <span class=u>②</span></a>
|
||||
<samp class=traceback>Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
AssertionError</samp></pre>
|
||||
AssertionError</samp>
|
||||
<a><samp class=p>>>> </samp><kbd class=pp>assert 2 + 2 == 5, "Only for very large values of 2"</kbd> <span class=u>③</span></a>
|
||||
<samp class=traceback>Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
AssertionError: Only for very large values of 2</samp></pre>
|
||||
<ol>
|
||||
<li>The <code>assert</code> statement is followed by any valid Python expression. In this case, the expression <code>1 + 1 == 2</code> evaluates to <code>True</code>, so the <code>assert</code> statement does nothing.
|
||||
<li>However, if the Python expression evaluates to <code>False</code>, the <code>assert</code> statement will raise an <code>AssertionError</code>.
|
||||
<li>You can also include a human-readable message that is printed if the <code>AssertionError</code> is raised.
|
||||
</ol>
|
||||
|
||||
<p>Therefore, this line of code:
|
||||
|
||||
<pre class=nd><code class=pp>assert len(unique_characters) <= 10</code></pre>
|
||||
<pre class=nd><code class=pp>assert len(unique_characters) <= 10, 'Too many letters'</code></pre>
|
||||
|
||||
<p>…is equivalent to…
|
||||
<p>…is equivalent to this:
|
||||
|
||||
<pre class=nd><code class=pp>if len(unique_characters) > 10:
|
||||
raise AssertionError</code></pre>
|
||||
raise AssertionError('Too many letters')</code></pre>
|
||||
|
||||
<p>But a bit easier to read and write.
|
||||
|
||||
<p>The alphametics solver uses this exact <code>assert</code> statement to bail out early if the puzzle contains more than ten unique letters. Since each letter is assigned a unique digit, and there are only ten digits, a puzzle with more than ten unique letters is unsolvable.
|
||||
<p>The alphametics solver uses this exact <code>assert</code> statement to bail out early if the puzzle contains more than ten unique letters. Since each letter is assigned a unique digit, and there are only ten digits, a puzzle with more than ten unique letters can not possibly have a solution.
|
||||
|
||||
<p class=a>⁂
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import itertools
|
||||
def solve(puzzle):
|
||||
words = re.findall('[A-Z]+', puzzle.upper())
|
||||
unique_characters = set(''.join(words))
|
||||
assert len(unique_characters) <= 10
|
||||
assert len(unique_characters) <= 10, 'Too many letters'
|
||||
first_letters = {word[0] for word in words}
|
||||
n = len(first_letters)
|
||||
sorted_characters = ''.join(first_letters) + \
|
||||
|
||||
Reference in New Issue
Block a user