gpt4 book ai didi

python - 如何将钱(以便士计)转换为单独的硬币?

转载 作者:太空狗 更新时间:2023-10-29 22:28:31 26 4
gpt4 key购买 nike

我的任务是
'编写一个函数 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)

我是编程新手,所以当我运行这段代码时,它只是输入
'1 0 0 0 0 0 0 0' 当我输入 292 而不是它应该输出的内容时。

最佳答案

由于您是编码新手,您应该开始在纸上编写您要遵循的过程,然后找出可以使用哪些工具来自动化此过程。

Important

Read the full answer in order!
Don't fall for the temptation of reading the code right away.

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).



算法

我要做的是:
  • 假设我有装有硬币的箱子,每个箱子都标有硬币面额。
    垃圾箱按面额从大到小排列,我总是从最高面额的垃圾箱中挑选所需数量的硬币,然后再移动到下一个垃圾箱。
  • 在一张纸上写下我需要计算的每种面额硬币数量的值(value)。
  • 从第一个垃圾箱(面额最高的那个)开始。
  • 从那个垃圾箱中挑选我需要的尽可能多的硬币,这样我就不会“超过”写在纸上的数量(注意这个数字可以为零)。
    这可以通过整数除法来完成;例如,如果您的值是 700,而 bin 的面额为 200,则计算整数除法 700 ÷ 200 = 3 (plus a remainder of 100)
  • 计算我挑选的硬币总数。
  • 删除在第 5 步中计算的值并将余数写为"new"值。
    由于您已经在步骤 4 中计算了整数除法,因此您可以计算余数。您还可以考虑在大多数编程语言中都有一个“模”运算符,可以立即为您提供整数除法的余数。使用上面的例子, 700 mod 200 = 100 ,它读取“700 modulo 200 是 100”,或“整数除法 700 ÷ 200 的余数是 100”。
  • 移动到下一个硬币箱。
  • 从第 4 步开始重复,直到我使用所有 bin 或值为零。

  • 例子

    假设我从 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)

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

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

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

    考虑算法:您重复从一个垃圾箱跳到下一个垃圾箱的步骤,并获得所需的硬币数量和剩余数量。这可以写成一个循环。

    因此,让我们将 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)

    就是这样!

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

    请注意,上面的代码仍然存在一个问题:您将 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)

    正如我的一个 friend 所说:“快速、准确、简洁;不缓慢、分散和困惑”

    TL; 博士
  • 在 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)

    Python 提供了很大的灵活性。随意尝试不同的方式来获得您需要的东西……然后选择更容易的方式。

    希望这可以帮助。

    关于python - 如何将钱(以便士计)转换为单独的硬币?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52706139/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com