python整钱兑换零钱while语句_关于python:如何将钱(便士)转换为单个硬币?

我的任务是

"编写一个功能selectCoins,要求用户输入金额

(以便士为单位),然后输出每种面额的硬币数量(从£ 2向下

到1p)应该用来精确地补足该金额(使用尽可能少的

硬币数量)。 例如,如果输入为292,则该函数应报告:

1×£ 2、0×£ 1、1×50p,2×20p,0×10p,0×5p,1×2p,0×1p。 (提示:使用整数

除法和余数)。"

def selectCoins():

twopound = 200

onepound = 100

fiftyp = 50

twentyp = 20

tenp = 10

fivep = 5

twop = 2

onep = 1

a = 0

b = 0

c = 0

d = 0

e = 0

f = 0

g = 0

h = 0

money = int(input('Enter how much money you have in pence'))

while True:

if money >= twopound:

money = money - twopound

a = a + 1

elif money >= onepound:

money = money - onepound

b = b + 1

elif money >= fiftyp:

money = money - fiftyp

c = c + 1

elif money >= twentyp:

money = money - twentyp

d = d + 1

elif money >= tenp:

money = money - tenp

e = e + 1

elif money >= fivep:

money = money - fivep

f = f + 1

elif money >= twop:

money = money - twop

g = g + 1

elif money >= onep:

money = money - onep

h = h + 1

else:

money = 0

break

print(a,b,c,d,e,f,g,h)

我是编程新手,因此在运行此代码时,它只是输入

当我键入292而不是它应输出的内容时,为" 1 0 0 0 0 0 0 0 0"。

可能是家庭作业,但问题很明确,提供了示例代码,并且答案也错误。 +1遵守规则。

问题很简单。 退还一磅两磅的硬币是正确的,但是这样做后您还没有完成。 然后,您应该检查下一个硬币要退还什么。 (可能是另外两磅的硬币)

乍一看,我想您需要将中断缩进到与else = 0相同的水平上。

那不是示例代码,而是我编写的代码。

您还没有使用提示:"使用整数除法和余数",并且其中有一个错字" moeny"。

Rup谢谢你,我做完了以后

是的,我不知道该怎么用。

由于您是编码的新手,因此应该开始编写在纸上执行的过程,然后找出可用于自动化该过程的工具。

好。

Important

Read the full answer in order!

Don't fall for the temptation of reading the code right away.

Ok.

The solutions I provide are hidden, but you can read them hovering your mouse over them or clicking on them (if you're using StackExchange mobile app, touch the"spoiler" link in each block).

Ok.

算法

我要做的是:

好。

假设我有一些装有硬币的垃圾箱,每个垃圾箱都标有硬币面额。

纸箱从最大面额到最小面额进行分类,在移到下一个纸箱之前,我总是从最大面额的筐中挑选尽可能多的硬币。

在纸上写下需要计算我需要的每种面额的硬币数量的值。

从第一个纸槽(具有最高面额的纸槽)开始。

从该容器中挑选我需要的硬币数量,以免我"超调"写在纸上的数量(注意该数字可以为零)。

这可以用整数除法来完成。例如,如果您的值为700,并且bin的面额为200,则您将计算整数除法700 ÷ 200 = 3 (plus a remainder of 100)

计算我挑选的硬币总数。

删除在第5步中计算出的值,并将其余值写为"新"值。

由于您已经在步骤4中计算了整数除法,因此可以计算余数。您还可以考虑在大多数编程语言中都有一个"模数"运算符,它将立即为您提供整数除法的余数。使用上面的示例,700 mod 200 = 100读取为" 700模200为100"或"整数除法运算除以700÷200的余数为100"。

移至下一个硬币箱。

从第4步开始重复,直到我使用了所有垃圾箱或该值为零。

假设我从292值开始,并且我有以下面额的容器(已经从最高面额到最低面额排序):

好。

|  200 |  100 |   50 |   20 |   10 |    5 |    2 |    1 |

+------+------+------+------+------+------+------+------+

|   I  |   II |  III |   IV |    V |   VI |  VII | VIII |

因此,让我们看看如果应用上述算法会发生什么:

好。

Write the value:   292

Start with the first bin (denomination: 200)

Pick 1 coin from the bin

The total amount picked from the bin is 200

The remainder is 92

Strike the previous value

The new value is 92

Move to the next bin (denomination: 100)

Pick 0 coins from the bin

The total amount picked from the bin is 0

The remainder is 92

Strike the previous value

The new value is 92

Move to the next bin (denomination: 50)

Pick 1 coin from the bin

The total amount picked from the bin is 50

The remainder is 42

Move to the next bin (denomination: 20)

Pick 2 coins from the bin

The total amount picked from the bin is 20

The remainder is 2

Move to the next bin (denomination: 10)

Pick 0 coins from the bin

The total amount picked from the bin is 0

The remainder is 2

Move to the next bin (denomination: 10)

Pick 0 coin from the bin

The total amount picked from the bin is 0

The remainder is 2

Move to the next bin (denomination: 5)

Pick 0 coin from the bin

The total amount picked from the bin is 0

The remainder is 2

Move to the next bin (denomination: 2)

Pick 1 coin from the bin

The total amount picked from the bin is 2

The remainder is 0

Done

用Python实现

Python是一种非常清晰的语言,可简化此类任务。因此,让我们尝试将我们的算法转换为Python。

好。

工具箱

假设您使用的是Python 3.x,则需要了解一些运算符:

好。

整数除法运算符(//):如果仅用一个斜杠除,将得到"实数除法"(例如3 / 2 == 1.5),但是如果使用双斜杠,则将得到"整数"除法(例如3 // 2 = 1)

取模运算符(%):如上所述,该运算符返回除法的余数(例如7 % 4 == 3)

好。

这些运算符一起使用,将为您提供每一步所需的信息:

好。

292 // 200 == 2

292 % 200 == 92

92 // 100 == 0

92 % 100 == 92

...

Python的一个有用特性是您可以执行"多重分配":您可以在一个步骤中将多个值分配给多个变量:

好。

# Initialize the value:

value = 292

# Initialize the denomination:

denomination = 200

# Calculate the amount of coins needed for the specified denomination

# and get the remainder (overwriting the value), in one single step:

coins, value = value // denomination, value % denomination

#              ^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^

#              |                      The remainder

#              The number of coins

#              (using integer division)

有了这些知识,我们可以编写解决方案:

好。

更正您的代码

切记:阅读以上所有内容,然后再揭示以下解决方案。

好。

def selectCoins():

twopound = 200

onepound = 100

fiftyp = 50

twentyp = 20

tenp = 10

fivep = 5

twop = 2

onep = 1

a = 0

b = 0

c = 0

d = 0

e = 0

f = 0

g = 0

h = 0

money = int(input('Enter how much money you have in pence')) # Example: 292

# Calculate the number of coins needed and the remainder

# The remainder will"overwrite" the value previously held in the"money" variable

a, money = money // twopound, money % twopound # a = 1, money = 92

b, money = money // onepound, money % onepound # b = 0, money = 92

c, money = money // fiftyp,   money % fiftyp   # c = 1, money = 42

d, money = money // twentyp,  money % twentyp  # d = 2, money = 2

e, money = money // tenp,     money % tenp     # e = 0, money = 2

f, money = money // fivep,    money % fivep    # f = 0, money = 2

g, money = money // twop,     money % twop     # g = 1, money = 0

e, money = money // onep,     money % onep     # e = 0, money = 0

print(a,b,c,d,e,f,g,h)

该解决方案使用整数除法和余数来执行计算。

好。

blockquote>

让我们以正确的方式来做:循环

让我们面对现实:上面的代码是冗长的。一定有更好的方法……而且有!使用循环。

考虑一下算法:重复执行从一个容器到下一个容器的步骤,并获取所需的硬币数量和剩余的硬币数量。这可以写成一个循环。

因此,让我们在工具箱中添加一个list:

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

然后将每个步骤的结果存储在第二个列表中:

coins = [] # We'll use the '.append()' method to add elements to this list

因此,从第一个" bin"开始:

n, money = money // denominations[0] , money % denominations[0]

coins.append(n)

让我们将其循环:

def select_coins_v2():

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

coins = []

money = int(input('Enter how much money you have in pence'))

for i in range(len(denominations)):

n, money = money // denominations[i], money % denominations[i]

coins.append(n)

print(coins)

就是这样!

好。

blockquote>

另一个改进:只获得一次面额两次使用

请注意,上面的代码仍然有问题:您两次阅读denominations。如果面额值只能读取一次,那就太好了。

当然,有一种方法:

def select_coins_v3():

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

coins = []

money = int(input('Enter how much money you have in pence'))

for d in denominations:  # 'd' will hold the value of the denomination

n, money = money // d, money % d

coins.append(n)

print(coins)

正如我的一个朋友所说:"快速,准确,简洁;不要缓慢,分散和混乱"

好。

blockquote>

TL; DR

在Python 3.x中,"整数除法"运算符为//,余数(模)运算符为%。

您可以在一行代码中执行多个分配:

a, b = 1, 2

您可以将面额存储在列表中:

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

您可以从面额列表中读取信息,并在单个步骤中获得整数除法和余数:

n, money = money // denominations[0], money % denominations[0]

您可以编写一个执行上述所有操作的循环:

for d in denominations: n, money = money // d, money % d

好。

奖励:使用字典

如果我想同时打印面额和使用的每种面额的硬币数量怎么办?您可以使用循环遍历两个列表,但是也可以使用字典来简化它:

def select_coins_v4():

denominations = [200, 100, 50, 20, 10, 5, 2, 1]

coins = []

money = int(input('Enter how much money you have in pence'))

for d in denominations:  # 'd' will hold the value of the denomination

n, money = money // d, money % d

coins.append(n)

number_of_coins = dict(zip(denominations, coins))

print(number_of_coins)

好。

blockquote>

Python提供了很大的灵活性。随意尝试各种获取所需内容的方式...,然后选择较简单的一种。

好。

希望这可以帮助。

好。

好。

我非常感谢您从skretch到完整的解决方案(包括用于示例的工具(操作员))解释问题的方式。只是一个校正..292 // 200将是1而不是2 ....

@ShivamSeth乐于帮助!在某一点上,我们所有人都遇到了这样的麻烦...我喜欢为所有帮助过我的人付出代价

太感谢您了,这是很大的帮助。当我变得更好时,我也会付钱。

@Nab Im很高兴您发现此答案很有帮助。我希望这可以为您提供解决方案的良好指导。很快您就会发现自己可以帮助其他人解决他们的问题。都是在这里帮助

@Nab我建议您看看官方的Python教程

关于使用实际面额的最酷的事情是贪婪的解决方案将始终找到最佳解决方案...对于奇怪的面额而言,这不再成立...但是,如果将它们分成几部分,这些问题总是最容易的

def get_one_change(amt_due):

# find how many of the largest denomination that you can use is

# ie for 60 = 1x50p is the count and number of largest

# for 4 = 4x1p ; 21 = 2x10p ; etc

return pence,count # ie 50,1 would be 1 50p coin

一旦有了这个,您只需要反复调用它并调整结果,直到没有必要更改为止

def get_change(amount_due):

changes_due = [] # to keep track

while amount_due:

pence,qty_of_coin = get_one_change(amount_due)

changes_due.append({"coin":pence,"qty":qty_of_coin})

amount_due = amount_due - (pence*qty_of_coin)

return changes_due

现在您可以使用用户输入来调用get_change方法

我有点理解您告诉我的第一部分,但后来我感到困惑。我是编码新手,所以这可能是原因。

您需要实现get_one_change

@Nab提示:在python中搜索整数除法和模。那会给你你所需要的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值