On Mon, Oct 12, 2009 at 11:33 AM, Victor Subervi <span dir="ltr"><<a href="mailto:victorsubervi@gmail.com">victorsubervi@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

    for row in data:<br>      i += 1<br>      total = 0<br>      quantity = form.getfirst('order_' + str(i), '')<br>      if quantity != '':<br>        sql = 'select * from products p join %s c on p.ID=c.ID where c.ID=%s;' % (client, str(i))<br>


        cursor.execute(sql)<br>        stuff = cursor.fetchone()<br>        price = str(int(stuff[5]*100))<br>        price = price[0:len(price)-2] + '.' + price[-2:]<br>        item, price, description, discount = stuff[2], price, stuff[3], stuff[8]<br>


        order += 'Item #: ' + item + '\tQuantity: ' + quantity + '\tPrice: ' + price + '\tDiscount: ' + str(discount) + '\tDescription: ' + description[:20] + '...<br />\n'<br>


        print 'Total 1: %s<br />' % total<br>        total += float(price) * int(quantity) * (100 - discount)/100<br>        print 'Total 2: %s<br />' % total<br>      order += 'TOTAL: $' + str(total)<br>


<br></blockquote><div><br></div><div>First, consider using the "decimal" module and not floats for money, so you don't get weird rounding. Floats are inexact. People usually prefer money to be exact.</div><div>

<br></div><div>Second, you initialize "total" to 0, and before "Total 1" is printed out-- you're never changing it unless I'm blind. You change it the first time below "Total 1".</div>

<div><br></div><div>Finally, consider being explicit with the "SELECT <columns> FROM <table>" and not using *. It lets you semi-future-proof your code verses future table changes, and is easier to maintain as you see in the code what order things come out and don't have to remember the order in the table itself.</div>

<div><br></div><div>HTH,</div><div><br></div><div>--S</div><div><br></div></div>