[Tutor] Difficulties with % operator

kromag@nsacom.net kromag@nsacom.net
Thu, 24 May 2001 14:05:16 -0700 (PDT)


Okay, I feel like a dolt...

A few weeks ago I asked some folks here about insertion of random numbers 
into a database. No problem! This was (one of)my answer(s):

"insert into pork values(%d, 'stuff', '1-2-1999')" % random.randint(1,1000))

No big deal, right?

Well, I have since been playing around with Abcess:

------------scriptish--------

import win32com.client
import random
import time
import string

engine=win32com.client.Dispatch("DAO.DBEngine.35")
db=engine.OpenDatabase("\windows\desktop\db2.mdb")
while 1:
	db.Execute("insert into food values(%d, %i, 'goat', 'pig', 'cow')" % 
time.time() %random.randint(1, 1000))

---------/scriptish-----------

Notice that I am getting fancy and attempting to do something semi-useful  
(inserting time...)

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)

so I tried variants like:

db.Execute("insert into food values(%d, %i, 'goat', 'pig', 'cow')" % 
(time.time() %random.randint(1, 1000)))

which looks unweildy and didn't work besides! :-)

Where am I going wrong?

d