It Doesn't Add Up!

Chris Kaynor ckaynor at zindagigames.com
Mon Oct 12 14:37:39 EDT 2009


Chris


On Mon, Oct 12, 2009 at 11:33 AM, Victor Subervi <victorsubervi at gmail.com>wrote:

> Hi;
> I have the following code:
>
>     for row in data:
>       i += 1
>       total = 0
>

In the above line, you're setting total to 0 each time the loop runs.


>       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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091012/99e46597/attachment-0001.html>


More information about the Python-list mailing list