改变柜台,不能分钱币

时间:2016-09-22 23:51:23

标签: python

我必须设计一个程序,当输入一定数量的项目时,询问用户支付的金额,然后提供20年代10的5个1s季度,硬币,镍币和硬币的变化。我花了好几个小时试图找到代码中的缺陷,但我无法做到正确。如果有人能帮助我,我将不胜感激!

我意识到我的代码不是很精致,但我刚开始并且到目前为止还没有学到太多技巧。 我把它全部工作直到它达到硬币,然后事情就变坏了。

此处是我的代码:

#assign variables#
import math
cost=float(input("Enter the price of the item: $"))
paid=float(input("Enter the amount paid: $"))
#calculations#
difference=round(paid,2)-round(cost,2)
change= round(difference,2)
#twentys#
remain20=float(change%20)
twent=float(change-remain20)
twent1=abs(float(twent)//20)
twent2=float(twent1*20)
sub1=float(change-float(twent2))
print(sub1,"sub1")
#tens

remain10=change%10
ten=sub1-remain10
ten1=ten//10
ten2=ten1*10
sub2=sub1-ten2
print(sub2,"sub2")
#fives

remain5=float(abs(change%5))
five=abs(float(sub2)-float(abs(remain5)))
five1=float(round(abs(five)//5))
five2=float(round(five*5))
sub3=abs(float(sub2)-abs(float(five2)))
print(sub3,"sub3")
#ones
remain1=change%1
one=abs(round(sub3)-abs(remain1))
one1=abs(round(one//1))
one2=abs(round(one*1))
sub4=abs(float(sub3))-abs(float(one2))
print(sub4,"sub4")
#quarters
remainq=change%(0.25)
remainq1=round(remainq,2)
q=abs(sub4)-(remainq1)
q1=abs(q//float(0.25))
q2=abs(q*0.25)
sub5=abs(float(sub4))-(float(q2))
print(sub5,"sub5")
#dimes

remaind=change%(0.10)
remaind1=round(remaind,2)
d=abs(round(sub5,2)-remaind1)
d1=abs(d//float(0.10))
d2=abs(d1*0.10)
sub6=abs(float(sub5))-abs(float(d2))
print(sub6,"sub6")
#nickles

remainn=change%(0.05)
remainn1=round(remainn,2)
n=abs(round(sub6,2)-abs(remainn1))
n1=abs(d//float(0.05))
n2=abs(d1*0.05)
sub7=float(abs(sub6)-float(n2))
print(sub7,"sub7")
#pennies

remainp=change%(0.01)
remainp1=round(remainp,2)
p=abs(round(sub7,2)-abs(remainp1))
p1=abs(d//float(0.01))
p2=abs(d1*0.01)
#outputs
print(round(twent1),str("Twenty dollar bills"))
print(round(ten1),str("Ten dollar bills"))
print(round(five1),str("Five dollar bills"))
print(round(one1),str("One dollar bills"))
print(round(q1),str("Quarters"))
print(int(d1),str("dimes"))
print(int(n1),str("nickles"))
print(int(p1),str("Pennies"))

3 个答案:

答案 0 :(得分:1)

如果可以用整数解决问题,我建议您避免使用浮点数。想想看,在这个特殊的问题中你可以将所有的ammounts转换成便士(乘以100)。这样,解决方案变得简单明了,例如:

def distribute(value):
    result = {
        2000: [0, "Twenty dollar"],
        1000: [0, "Ten dollar"],
        500: [0, "Five dollar"],
        100: [0, "One dollar"],
        25: [0, "Quarters"],
        10: [0, "dimes"],
        5: [0, "nickles"],
        1: [0, "Pennies"]
    }

    if value < 0:
        print("Not enough money... you need {0}$ more".format(abs(value)))
    elif value == 0:
        print("Thanks for buying in the shop!")
    else:
        pennies = value * 100

        for k in reversed(sorted(result.keys())):
            if k < pennies:
                result[k][0] = num_coins = int(pennies / k)
                print("{0} {1}{2}".format(
                    num_coins, result[k][1], " bills" if k >= 100 else ""))
                pennies -= num_coins * k

    return result

if __name__ == "__main__":
    try:
        print("---------------------------")
        print(" Welcome to shopping 0.0.1 ")
        print("---------------------------")
        cost = int(input("Enter the price of the item: $"))
        paid = int(input("Enter the amount paid: $"))
        summary = distribute(paid - cost)
    except Exception as e:
        print("Something unexpected happened! {0}".format(e))

答案 1 :(得分:0)

您的主要问题是货币计算(和剩余操作)与float的效果不佳。预先转换为int个便士,并且大多数错误来源都会消失(只要你正确地绕圈)。

您还可以大量简化代码。现在,每个面额都是它自己的特殊情况,尽管除了字符串名称和单位之外,所有这些都是基本相同的。概括代码,你可以将它简化为一个简单的循环,其中有一对(常量,顶级)tuple定义实际改变的部分:

# These are the only thing that differ as you go, so just list them explicitly
denominations = ('Twenty dollar bills', 'Ten dollar bills', 'Five dollar bills', 'One dollar bills', 'quarters', 'dimes', 'nickels', 'pennies')
divisors = (2000, 1000, 500, 100, 25, 10, 5, 1)

def make_change(paid, cost):
    '''paid and cost should be floats with up to two decimal places of precision'''
    # Calculate the difference in price, then convert to an int penny count
    # so we can use accurate math
    change = round((paid - cost) * 100)  # Wrap round in int constructor on Py2

    # Not necessary if inputs are trusted, but good form
    if change < 0: raise ValueError("Underpaid!")

    # Each loop replaces a special case from your code by swapping in the
    # string and divisor for that round
    for denom, div in zip(denominations, divisors):
        numdenom, change = divmod(change, div)
        # Conditional print; weird to say "0 fives", "0 ones" over and over
        # You can print unconditionally if you prefer
        if numdenom:
            print(numdenom, denom)

就是这样。整个计算是(忽略注释和空行)两个常量定义和七行实际代码。

答案 2 :(得分:-1)

import math

def to_pennies(amt):
  return int(math.floor(amt * 100))

def make_change(amt):
  pennies = to_pennies(amt)
  twenties = pennies % 2000
  pennies -= twenties * 2000
  fives = pennies % 500
  pennies -= fives * 500
  ...  # Fill in the rest
  nickels = pennies % 5
  pennies -= nickels * 5
  return dict(
    twenties=twenties,
    fives=fives,
    ... # fill in the rest
    nickels=nickels,
    pennies=pennies)