It Doesn't Add Up!

Victor Subervi victorsubervi at gmail.com
Mon Oct 12 20:33:31 CEST 2009


Hi;
I have the following code:

    for row in data:
      i += 1
      total = 0
      quantity = form.getfirst('order_' + str(i), '')
      if quantity != '':
        sql = 'select * from products p join %s c on p.ID=c.ID where
c.ID=%s;' % (client, str(i))
        cursor.execute(sql)
        stuff = cursor.fetchone()
        price = str(int(stuff[5]*100))
        price = price[0:len(price)-2] + '.' + price[-2:]
        item, price, description, discount = stuff[2], price, stuff[3],
stuff[8]
        order += 'Item #: ' + item + '\tQuantity: ' + quantity + '\tPrice: '
+ price + '\tDiscount: ' + str(discount) + '\tDescription: ' +
description[:20] + '...<br />\n'
        print 'Total 1: %s<br />' % total
        total += float(price) * int(quantity) * (100 - discount)/100
        print 'Total 2: %s<br />' % total
      order += 'TOTAL: $' + str(total)

It prints out the following to screen with the print statements:

Total 1: 0
Total 2: 1.98
Total 1: 0
Total 2: 6.3

As you can see, the total doesn't accumulate! There are two rows. The second
"Total 1:" should show "1.98", not 0! What gives?
TIA,
Victor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091012/59f9f5a6/attachment.html>


More information about the Python-list mailing list