praiseHellRaiseDale
praiseHellRaiseDale

Reputation: 2270

Python, first time using Decimal and quantize

I was just wondering if anybody had any input on how to improve this code. My goal is for it to be as pythonic as possible since I'm trying to really learn python well. This program works fine, but if you see anything that you think could be done to improve (not major changes, just basic "Im new to python" stuff) this program please let me know.

#!/usr/bin/python
from decimal import *


print "Welcome to the checkout counter!  How many items are you purchasing today?"

numOfItems = int(raw_input())

dictionary = {}

for counter in range(numOfItems):

    print "Please enter the name of product", counter + 1
    currentProduct = raw_input()    

    print "And how much does", currentProduct, "cost?"
    currentPrice = float(raw_input())

    dictionary.update({currentProduct:currentPrice})

print "Your order was:"

subtotal = 0
for key, value in dictionary.iteritems():

    subtotal = subtotal + value
    stringValue = str(value)
    print key, "$" + stringValue

tax = subtotal * .09
total = subtotal + tax
total = Decimal(str(total)).quantize(Decimal('0.01'), rounding = ROUND_DOWN)

stringSubtotal = str(subtotal)
stringTotal = str(total)

print "Your subtotal comes to", "$" + stringSubtotal + ".", " With 9% sales tax, your total is $" + stringTotal + "."

print "Please enter cash amount:"
cash = Decimal(raw_input()).quantize(Decimal('0.01'))

change = cash - total
stringChange = str(change)

print "I owe you back", "$" + stringChange

print "Thank you for shopping with us!"

Upvotes: 1

Views: 1945

Answers (3)

nneonneo
nneonneo

Reputation: 179422

  1. Call the product dictionary "products" or some similarly descriptive name, instead of just "dictionary"
  2. Generally, if you are iterating over a range, use xrange instead of range for better performance (though it's a very minor nitpick in an app like this)
  3. You can use subtotal = sum(dictionary.itervalues()) to quickly add up all the item prices, without having to use the loop.
  4. You should definitely use Decimal throughout to avoid inaccuracies due to float.
  5. You can use a formatting string like '%.2f' % value (old-style format) or '{:.2f}' .format(value) (new-style format) to print out values with two decimal places.
  6. The tax value should be a constant, so it can be changed easily (it's used in two places, once for the calculation and once for the display).

Upvotes: 3

HYRY
HYRY

Reputation: 97291

1 to add key-value in a dict, you can use:

dictionary[currentProduct] = currentPrice

but, you don't need a dict in this case, because dict is orderless. You can use a list of tuple.

2 Why not use Decimal(raw_input()), then you can do all the calculation in decimal without using floats.

3 To print the result, you don't need convert values to str first, you can use str.format()

Upvotes: 1

ACEfanatic02
ACEfanatic02

Reputation: 704

  • Updating a dictionary, I would use dict[key] = value, rather than dict.update({key:value})

  • Instead of concatenating strings, try using format specification. This looks cleaner and saves you having to convert values to strings explicitly.

    • C-style: "Qty: %d, Price: %f" % (qty, price)
    • string.format: "Qty: {0}, Price {1}".format(qty, price)

Upvotes: 1

Related Questions