Re: It Doesn't Add Up!
On Mon, Oct 12, 2009 at 11:33 AM, Victor Subervi wrote: > 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] + '...\n' > print 'Total 1: %s' % total > total += float(price) * int(quantity) * (100 - discount)/100 > print 'Total 2: %s' % total > order += 'TOTAL: $' + str(total) > > 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. 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". Finally, consider being explicit with the "SELECT FROM " 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. HTH, --S -- http://mail.python.org/mailman/listinfo/python-list
Re: It Doesn't Add Up!
Victor Subervi wrote: Hi; I have the following code: [snip] price = str(int(stuff[5]*100)) price = price[0:len(price)-2] + '.' + price[-2:] [snip] This is simpler: price = "%.2f" % stuff[5] (not that I like what you're doing with it; leaving it as a float and just formatting to 2 decimal places when printing it would be better! :-)) -- http://mail.python.org/mailman/listinfo/python-list
Re: It Doesn't Add Up!
Ouch! You're right! <;) V On Mon, Oct 12, 2009 at 1:38 PM, Rami Chowdhury wrote: > On Mon, 12 Oct 2009 11:33:31 -0700, Victor Subervi < > victorsube...@gmail.com> wrote: > > Hi; >> I have the following code: >> >>for row in data: >> i += 1 >> total = 0 >> > [snip] > >> 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? >> > > You are setting total = 0 inside the for loop, so it is getting rebound for > every row... > > -- http://mail.python.org/mailman/listinfo/python-list
Re: It Doesn't Add Up!
On Mon, 12 Oct 2009 11:33:31 -0700, Victor Subervi wrote: Hi; I have the following code: for row in data: i += 1 total = 0 [snip] 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? You are setting total = 0 inside the for loop, so it is getting rebound for every row... -- http://mail.python.org/mailman/listinfo/python-list
Re: It Doesn't Add Up!
Chris On Mon, Oct 12, 2009 at 11:33 AM, Victor Subervi 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] + '...\n' > print 'Total 1: %s' % total > total += float(price) * int(quantity) * (100 - discount)/100 > print 'Total 2: %s' % 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 > > -- http://mail.python.org/mailman/listinfo/python-list
It Doesn't Add Up!
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] + '...\n' print 'Total 1: %s' % total total += float(price) * int(quantity) * (100 - discount)/100 print 'Total 2: %s' % 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