Python 列出给定数量的硬币需要哪些硬币
我需要编写一个函数,以列表格式打印给定金额所需的英国硬币数量(即列表中的8个值,分别为2英镑、1英镑、0.50英镑、0.20英镑、0.10英镑、0.05英镑、0.02英镑和0.01英镑) 到目前为止,我写了以下内容:Python 列出给定数量的硬币需要哪些硬币,python,Python,我需要编写一个函数,以列表格式打印给定金额所需的英国硬币数量(即列表中的8个值,分别为2英镑、1英镑、0.50英镑、0.20英镑、0.10英镑、0.05英镑、0.02英镑和0.01英镑) 到目前为止,我写了以下内容: def pay_with_coins( amount ): coins_list = [0, 0, 0, 0, 0, 0, 0, 0] if amount == 0: return(coins_list) else: while amount > 2.00
def pay_with_coins( amount ):
coins_list = [0, 0, 0, 0, 0, 0, 0, 0]
if amount == 0:
return(coins_list)
else:
while amount > 2.00:
coins_list[0] = (coins_list[0] + 1)
amount = amount - 2.00
while amount >= 1.00 and amount < 2.00:
coins_list[1] = (coins_list[1] + 1)
amount = amount - 1.00
while amount >= 0.50 and amount < 1.00:
coins_list[2] = (coins_list[2] + 1)
amount = amount - 0.50
while amount >= 0.20 and amount < 0.50:
coins_list[3] = (coins_list[3] + 1)
amount = amount - 0.20
while amount >= 0.10 and amount < 0.20:
coins_list[4] = (coins_list[4] + 1)
amount = amount - 0.10
while amount >= 0.05 and amount < 0.10:
coins_list[5] = (coins_list[5] + 1)
amount = amount - 0.05
while amount >= 0.02 and amount < 0.05:
coins_list[6] = (coins_list[6] + 1)
amount = amount - 0.02
while amount >= 0.01 and amount < 0.05:
coins_list[7] = (coins_list[7] + 1)
amount = amount - 0.01
return(coins_list)
print(pay_with_coins(0.08))
print(pay_with_coins(8.02))
print(pay_with_coins(1.74))
print(pay_with_coins(1001))
这就是我应该得到的:
[0,0,0,0,0,1,1,1]
[4,0,0,0,0,0,1,0]
[0,1,1,1,0,0,2,0]
[500,1,0,0,0,0,0,0]
这就是我实际得到的:
[0, 0, 0, 0, 0, 1, 1, 0]
[4, 0, 0, 0, 0, 0, 0, 1]
[0, 1, 1, 1, 0, 0, 1, 1]
[500, 1, 0, 0, 0, 0, 0, 0]
正如您所看到的,列表中的最后两个值似乎出现了问题,我不太确定问题出在哪里
我感觉最后两个值搞乱了,因为它们是0.05和0.01(小数点后两位)。你知道怎么分类吗?你要找的是一种把一个数字分成越来越小的数字的方法。一种方法是通过
divmod
def pay_with_coins(amount):
twoer,rest=divmod(amount, 2) # £2
onner,rest=divmod(rest, 1) # £1
halfer,rest=divmod(rest, 0.5) # £0.50
fifther,rest=divmod(rest, 0.20) # £0.20
tenther,rest=divmod(rest, 0.10) # £0.10
twenthier,rest=divmod(rest, 0.05) # £0.05
fifthier,rest=divmod(rest, 0.02) # £0.02
hundreder,rest=divmod(rest,0.01) # £0.01
coinList = [twoer, onner, halfer, fifther, tenther, twenthier,fifthier, hundreder]
return [i for i in map(int, coinList)]
在上面的代码中,我对同一个变量连续使用divmod
,将值分为低值和低值
在为这个问题创建“解决方案”之前,我似乎没有完全阅读这个问题。阅读这个问题的另一个答案,他们建议用美分进行计算,以避免使用不必要的浮点数,我还编辑了我的代码,如下所示:
def pay_with_coins(amount):
amount *= 100
twoer,rest=divmod(amount,200) # £2
onner,rest=divmod(rest,100) # £1
halfer,rest=divmod(rest,50) # £0.50
fifther,rest=divmod(rest,20) # £0.20
tenther,rest=divmod(rest,10) # £0.10
twenthier,rest=divmod(rest,5) # £0.05
fifthier,rest=divmod(rest,2) # £0.02
hundreder,rest=divmod(rest,1) # £0.01
coinList = [twoer, onner, halfer, fifther, tenther, twenthier,fifthier, hundreder]
return [i for i in map(int, coinList)]
唯一真正的区别是,我将给定的数量乘以100,并对计算进行了相同的操作,因此避免同时使用浮点数。您要寻找的是一种将数字划分为越来越小的数字的方法。一种方法是通过
divmod
def pay_with_coins(amount):
twoer,rest=divmod(amount, 2) # £2
onner,rest=divmod(rest, 1) # £1
halfer,rest=divmod(rest, 0.5) # £0.50
fifther,rest=divmod(rest, 0.20) # £0.20
tenther,rest=divmod(rest, 0.10) # £0.10
twenthier,rest=divmod(rest, 0.05) # £0.05
fifthier,rest=divmod(rest, 0.02) # £0.02
hundreder,rest=divmod(rest,0.01) # £0.01
coinList = [twoer, onner, halfer, fifther, tenther, twenthier,fifthier, hundreder]
return [i for i in map(int, coinList)]
在上面的代码中,我对同一个变量连续使用divmod
,将值分为低值和低值
在为这个问题创建“解决方案”之前,我似乎没有完全阅读这个问题。阅读这个问题的另一个答案,他们建议用美分进行计算,以避免使用不必要的浮点数,我还编辑了我的代码,如下所示:
def pay_with_coins(amount):
amount *= 100
twoer,rest=divmod(amount,200) # £2
onner,rest=divmod(rest,100) # £1
halfer,rest=divmod(rest,50) # £0.50
fifther,rest=divmod(rest,20) # £0.20
tenther,rest=divmod(rest,10) # £0.10
twenthier,rest=divmod(rest,5) # £0.05
fifthier,rest=divmod(rest,2) # £0.02
hundreder,rest=divmod(rest,1) # £0.01
coinList = [twoer, onner, halfer, fifther, tenther, twenthier,fifthier, hundreder]
return [i for i in map(int, coinList)]
唯一真正的区别是,我将给定的数量乘以100,并对计算进行了相同的操作,因此避免同时使用浮点数。啊,我担心这是了解二进制系统在浮点运算方面的局限性的最糟糕的方法之一 用二进制表示法准确地表示每个十进制数是不可能的。 为了避免这个问题,当涉及到货币时,使用美分作为基本单位,并完全避免浮动
def pay_with_coins( amount_in_cents ):
coins_list = [0, 0, 0, 0, 0, 0, 0, 0]
if amount_in_cents == 0:
return(coins_list)
else:
while amount_in_cents > 200:
coins_list[0] = (coins_list[0] + 1)
amount_in_cents = amount_in_cents - 200
while amount_in_cents >= 100 and amount_in_cents < 200:
coins_list[1] = (coins_list[1] + 1)
amount_in_cents = amount_in_cents - 100
while amount_in_cents >= 50 and amount_in_cents < 100:
coins_list[2] = (coins_list[2] + 1)
amount_in_cents = amount_in_cents - 50
while amount_in_cents >= 20 and amount_in_cents < 50:
coins_list[3] = (coins_list[3] + 1)
amount_in_cents = amount_in_cents - 20
while amount_in_cents >= 10 and amount_in_cents < 20:
coins_list[4] = (coins_list[4] + 1)
amount_in_cents = amount_in_cents - 10
while amount_in_cents >= 5 and amount_in_cents < 10:
coins_list[5] = (coins_list[5] + 1)
amount_in_cents = amount_in_cents - 5
while amount_in_cents >= 2 and amount_in_cents < 5:
coins_list[6] = (coins_list[6] + 1)
amount_in_cents = amount_in_cents - 2
while amount_in_cents >= 1 and amount_in_cents < 2:
coins_list[7] = (coins_list[7] + 1)
amount_in_cents = amount_in_cents - 1
return(coins_list)
def用硬币支付(金额单位:美分):
硬币列表=[0,0,0,0,0,0,0,0,0,0]
如果金额(单位:美分)=0:
退货(硬币清单)
其他:
当金额(单位:美分)大于200时:
硬币列表[0]=(硬币列表[0]+1)
金额单位=金额单位-200
当金额(单位:美分)大于等于100且金额(单位:美分)小于200时:
硬币清单[1]=(硬币清单[1]+1)
金额单位=金额单位-100
当金额(美分)>=50且金额(美分)<100时:
硬币清单[2]=(硬币清单[2]+1)
金额单位=金额单位-50
当金额(单位:美分)大于等于20且金额(单位:美分)小于50时:
硬币清单[3]=(硬币清单[3]+1)
金额单位=金额单位-20
当金额(美分)>=10且金额(美分)<20时:
硬币清单[4]=(硬币清单[4]+1)
金额单位=金额单位-10
当金额(单位:美分)大于等于5且金额(单位:美分)小于10时:
硬币清单[5]=(硬币清单[5]+1)
金额单位=金额单位-5
当金额(单位:美分)大于等于2且金额(单位:美分)小于5时:
硬币清单[6]=(硬币清单[6]+1)
金额单位=金额单位-2
当金额(单位:美分)大于等于1且金额(单位:美分)小于2时:
硬币清单[7]=(硬币清单[7]+1)
金额单位=金额单位-1
退货(硬币清单)
啊,我担心这是了解二进制系统在浮点运算方面的局限性的最糟糕的方法之一
用二进制表示法准确地表示每个十进制数是不可能的。
为了避免这个问题,当涉及到货币时,使用美分作为基本单位,并完全避免浮动
def pay_with_coins( amount_in_cents ):
coins_list = [0, 0, 0, 0, 0, 0, 0, 0]
if amount_in_cents == 0:
return(coins_list)
else:
while amount_in_cents > 200:
coins_list[0] = (coins_list[0] + 1)
amount_in_cents = amount_in_cents - 200
while amount_in_cents >= 100 and amount_in_cents < 200:
coins_list[1] = (coins_list[1] + 1)
amount_in_cents = amount_in_cents - 100
while amount_in_cents >= 50 and amount_in_cents < 100:
coins_list[2] = (coins_list[2] + 1)
amount_in_cents = amount_in_cents - 50
while amount_in_cents >= 20 and amount_in_cents < 50:
coins_list[3] = (coins_list[3] + 1)
amount_in_cents = amount_in_cents - 20
while amount_in_cents >= 10 and amount_in_cents < 20:
coins_list[4] = (coins_list[4] + 1)
amount_in_cents = amount_in_cents - 10
while amount_in_cents >= 5 and amount_in_cents < 10:
coins_list[5] = (coins_list[5] + 1)
amount_in_cents = amount_in_cents - 5
while amount_in_cents >= 2 and amount_in_cents < 5:
coins_list[6] = (coins_list[6] + 1)
amount_in_cents = amount_in_cents - 2
while amount_in_cents >= 1 and amount_in_cents < 2:
coins_list[7] = (coins_list[7] + 1)
amount_in_cents = amount_in_cents - 1
return(coins_list)
def用硬币支付(金额单位:美分):
硬币列表=[0,0,0,0,0,0,0,0,0,0]
如果金额(单位:美分)=0:
退货(硬币清单)
其他:
当金额(单位:美分)大于200时:
硬币列表[0]=(硬币列表[0]+1)
金额单位=金额单位-200
当金额(单位:美分)大于等于100且金额(单位:美分)小于200时:
硬币清单[1]=(硬币清单[1]+1)
金额单位=金额单位-100
当金额(美分)>=50且金额(美分)<100时:
硬币清单[2]=(硬币清单[2]+1)
金额单位=金额单位-50
当金额(单位:美分)大于等于20且金额(单位:美分)小于50时:
硬币清单[3]=(硬币清单[3]+1)
金额单位=金额单位-20
当金额(美分)>=10且金额(美分)<20时:
硬币清单[4]=(硬币清单[4]+1)
金额单位=金额单位-10
当金额(单位:美分)大于等于5且金额(单位:美分)小于10时:
硬币清单[5]=(硬币清单[5]+1)
金额单位=金额单位-5
当金额(单位:美分)大于等于2且金额(单位:美分)小于5时:
硬币清单[6]=(硬币清单[6]+1)
金额单位=金额单位-2
当金额(单位:美分)大于等于1且金额(单位:美分)小于2时:
硬币清单[7]=(硬币清单[7]+1)
金额单位=金额单位-1
退货(硬币清单)
正如@Paritosh Singh在他的回答中所说,浮动存在问题。但是,如果您想要一个更具可扩展性的解决方案,您可以尝试以下方法,这将节省大量的输入
# Create list of currencies
currencies = [2.00, 1.00, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01]
def pay_with_coins(amount):
# Initialize array
coins = [0 for i in range(len(currencies))]
# Adjust to integers to avoid floating point issues
amount = int(amount * 100)
values = [c * 100 for c in currencies]
# Loop throug values
for currency in values:
i = values.index(currency)
coins[i] = 0
# Dish out coins until you need to move to a smaller value
while amount >= currency:
amount -= currency
coins[i] += 1
return coins
print(pay_with_coins(0.08)) #[0, 0, 0, 0, 0, 1, 1, 1]
print(pay_with_coins(8.02)) #[4, 0, 0, 0, 0, 0, 1, 0]
print(pay_with_coins(1.74)) #[0, 1, 1, 1, 0, 0, 2, 0]
print(pay_with_coins(1001)) #[500, 1, 0, 0, 0, 0, 0, 0]
正如@Paritosh Singh在他的回答中所说,浮动存在一个问题。但是,如果您想要一个更具可扩展性的解决方案,您可以尝试以下方法,这将节省大量的输入
# Create list of currencies
currencies = [2.00, 1.00, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01]
def pay_with_coins(amount):
# Initialize array
coins = [0 for i in range(len(currencies))]
# Adjust to integers to avoid floating point issues
amount = int(amount * 100)
values = [c * 100 for c in currencies]
# Loop throug values
for currency in values:
i = values.index(currency)
coins[i] = 0
# Dish out coins until you need to move to a smaller value
while amount >= currency:
amount -= currency
coins[i] += 1
return coins
print(pay_with_coins(0.08)) #[0, 0, 0, 0, 0, 1, 1, 1]
print(pay_with_coins(8.02)) #[4, 0, 0, 0, 0, 0, 1, 0]
print(pay_with_coins(1.74)) #[0, 1, 1, 1, 0, 0, 2, 0]
print(pay_with_coins(1001)) #[500, 1, 0, 0, 0, 0, 0, 0]
您可以使用Python的-模块来实现这一点。
它以小数(基数)表示数字