【发布时间】:2013-01-12 04:40:24
【问题描述】:
我只是想知道是否有人对如何改进此代码有任何意见。我的目标是让它尽可能像 Python 一样,因为我正在努力真正学好 Python。该程序运行良好,但如果您发现任何您认为可以改进的地方(不是重大更改,只是基本的“我是 python 新手”),请告诉我。
#!/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!"
【问题讨论】:
-
我认为你的程序需要一些异常处理来处理非法输入
-
更适合 codereview.sx?
-
@nneonneo 是的,它可能更适合 codereview.sx,但我想,因为我是新手,所以可能会出现一些严重的错误,可能会带来更有趣的方法事物。但你可能是对的。
标签: python string dictionary decimal