python第一次使用_Python,第一次使用Decimal和quantiz

这是一个Python初学者编写的购物结账程序,利用Decimal处理精度问题,并计算9%的销售税。用户输入商品名称及价格,程序计算小计、总价并找零。寻求代码改进意见。
摘要由CSDN通过智能技术生成

我只是想知道是否有人对如何改进这个代码有任何意见。我的目标是让它尽可能的像Python一样,因为我正在努力真正学好python。这个程序运行得很好,但是如果你看到任何你认为可以改进的地方(不是重大的改变,只是一些基本的“imnewtopython”的东西),请让我知道。在#!/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!"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值