[Tutor] Difficulties with % operator
D-Man
dsh8290@rit.edu
Thu, 24 May 2001 15:34:30 -0400
On Thu, May 24, 2001 at 02:05:16PM -0700, kromag@nsacom.net wrote:
...
| db.Execute("insert into food values(%d, %i, 'goat', 'pig', 'cow')" %
| time.time() %random.randint(1, 1000))
...
| Unfortunately, I get the following error message:
|
| c:windowsdesktop>python test.py
| Traceback (most recent call last):
| File "test.py", line 9, in ?
| db.Execute("insert into food values(%d, %i, 'goat', 'pig', 'cow')" %
| time.time() %random.randint(1, 1000))
| TypeError: not enough arguments for format string
|
|
| Now my copy of Learning Python gives an example:
|
| 'That is %d %s bird' % (1, dead)
This string has 2 format operators (%d and %s). After the string
follows the string interpolation operator '%'. After that is a single
_tuple_ who size is the same as the number of format operators in the
string and it contains the values to be inserted into the string.
(excess stuff removed)
| "insert into food values(%d, %i, 'goat', 'pig', 'cow')" % \
| time.time() % random.randint(1, 1000)
Here you have a string that also has 2 format operators (%d and %i).
Following it you have the string interpolation operator, then a bit
different expression.
| time.time() % random.randint(1, 1000)
Here you are getting the time and a random number, then performing
modulo division on it which results in a single float. Ex:
>>> time.time() % random.randint( 1 , 1000 )
248.07400000095367
What you mean instead is to have a 2-tuple containing the result of
each function :
( time.time() , random.randint(1, 1000) )
So the line should read :
db.Execute("insert into food values(%d, %i, 'goat', 'pig', 'cow')" %
( time.time() , random.randint(1, 1000) ) )
HTH,
-D