Chapter 4, Tasklist II: Databases and AJAX

Using variable selection criteria

Answer:

cursor.execute('select * from task where user_id = ?',(username,))

A working implementation is available as taskdb3.py. Note that because there may be more than one placeholder present in a query, we pass the actual values for these placeholders as a tuple. A peculiarity of the Python syntax demands that a tuple is defined as parentheses containing a comma separated list of expressions and that a tuple consisting of a single item still has to contain a comma. (username,) is, therefore, a tuple with a single item.

Spotting the error

Answer:

test_number()

It will fail in the very first assertion with an output like the following:

python.exe test_factorial.py
.F.
======================================================================
FAIL: test_number (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_factorial.py", line 7, in test_number
self.assertEqual(24,fac(4))
AssertionError: 24 != 12
----------------------------------------------------------------------
Ran 3 tests in 0.094s
FAILED (failures=1)

It still doesn't say what is wrong in the code, but now you know that the new implementation does not calculate the factorial of a number correctly. The solution might not be hard to spot this time: the range() function should be passed 2 as its first argument, because only 0 and 1 are treated as special-case in the code

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.111.208