返回找零:如何从“找零”中删除变量(美元、25 美分、10 角、镍币、便士)?如果变量=0?

发布于 2025-01-16 23:14:23 字数 1849 浏览 3 评论 0原文

Zybooks 4.18 LAB:精确找零

编写一个程序,以总找零金额作为整数输入,并使用最少的硬币输出找零,每行一种硬币类型。硬币类型有美元、25 美分、10 美分、5 美分和便士。适当使用单数和复数硬币名称,例如 1 Penny 与 2 Pennies。

我想知道是否可以使用任何东西来代替 change.remove() 来从“change”中删除该变量。我知道 change.remove() 不起作用,但将其放在那里以显示我想要做什么。我现在正在上第一门计算机科学课,所以请记住,有很多东西我可能还没有学到。我知道这个问题之前已经在这里解决过,但我正在尝试用我迄今为止所学到的知识编写代码。有什么方法可以使用我所拥有的东西来解决这个问题,并且只修复一些元素?这就是我现在所拥有的:

x=int(input())
if x<=0:
    print('No change')
else:
    num_of_dollars = x//100
    x = x-num_of_dollars*100
    num_of_quarters = x//25
    x = x-num_of_quarters*25
    num_of_dimes = x//10
    x = x-num_of_dimes*10
    num_of_nickels = x//5
    x = x-num_of_nickels*5
    num_of_pennies = x//1

    change = dollars+'\n'+quarters+'\n'+dimes+'\n'+nickels+'\n'+pennies

    if num_of_dollars>1:
        dollars = str(num_of_dollars)+' Dollars'
    elif num_of_dollars==1:
        dollars = str(num_of_dollars)+' Dollar'
    elif num_of_dollars==0:
        change.remove(dollars)
    
    if num_of_quarters>1:
        quarters = str(num_of_quarters)+' Quarters'
    elif num_of_quarters==1:
        quarters = str(num_of_quarters)+' Quarter'
    elif num_of_quarters==0:
        change.remove(quarters)

    if num_of_dimes>1:
        dimes = str(num_of_dimes)+' Dimes'
    elif num_of_dimes==1:
        dimes = str(num_of_dimes)+' Dime'
    elif num_of_dimes==0:
        change.remove(dimes)
    
    if num_of_nickels>1:
        nickels = str(num_of_nickels)+' Nickels'
    elif num_of_nickels==1:
        nickels = str(num_of_nickels)+' Nickel'
    elif num_of_nickels==0:
        change.remove(nickels)
    
    if num_of_pennies>1:
        pennies = str(num_of_pennies)+' Pennies'
    elif num_of_pennies==1:
        pennies = str(num_of_pennies)+' Penny'
    elif num_of_pennies==0:
        change.remove(pennies)

    print(change)

Zybooks 4.18 LAB: Exact change

Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies.

I want to know if there's anything I can use in place of change.remove() to remove that variable from "change." I know that change.remove() doesn't work but put it there to show what it is that I want to do. I'm taking my first Computer Science class right now, so keep in mind that there are a lot of things I might not have learned yet. I know that this problem has been solved before on here before, but I'm trying to write a code with what I have learned so far. Is there any way that I could do this problem using what I have and only fixing a few elements? This is what I have right now:

x=int(input())
if x<=0:
    print('No change')
else:
    num_of_dollars = x//100
    x = x-num_of_dollars*100
    num_of_quarters = x//25
    x = x-num_of_quarters*25
    num_of_dimes = x//10
    x = x-num_of_dimes*10
    num_of_nickels = x//5
    x = x-num_of_nickels*5
    num_of_pennies = x//1

    change = dollars+'\n'+quarters+'\n'+dimes+'\n'+nickels+'\n'+pennies

    if num_of_dollars>1:
        dollars = str(num_of_dollars)+' Dollars'
    elif num_of_dollars==1:
        dollars = str(num_of_dollars)+' Dollar'
    elif num_of_dollars==0:
        change.remove(dollars)
    
    if num_of_quarters>1:
        quarters = str(num_of_quarters)+' Quarters'
    elif num_of_quarters==1:
        quarters = str(num_of_quarters)+' Quarter'
    elif num_of_quarters==0:
        change.remove(quarters)

    if num_of_dimes>1:
        dimes = str(num_of_dimes)+' Dimes'
    elif num_of_dimes==1:
        dimes = str(num_of_dimes)+' Dime'
    elif num_of_dimes==0:
        change.remove(dimes)
    
    if num_of_nickels>1:
        nickels = str(num_of_nickels)+' Nickels'
    elif num_of_nickels==1:
        nickels = str(num_of_nickels)+' Nickel'
    elif num_of_nickels==0:
        change.remove(nickels)
    
    if num_of_pennies>1:
        pennies = str(num_of_pennies)+' Pennies'
    elif num_of_pennies==1:
        pennies = str(num_of_pennies)+' Penny'
    elif num_of_pennies==0:
        change.remove(pennies)

    print(change)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2025-01-23 23:14:23

随心所欲地添加到 change 似乎比删除任何内容更简单,特别是因为它的初始分配确实不应该起作用,并且不保留与用于组成 change 的变量的连接

例如

    change = ``

    if num_of_dollars > 1:
        dollars = str(num_of_dollars)+' Dollars\n'
    elif num_of_dollars == 1:
        dollars = str(num_of_dollars)+' Dollar\n'
    if num_of_dollars > 0:
        change += dollars
    

等等 - 我试图尽可能地保持你的结构。

:

但是当你了解列表和列表的列表时:

coins = [["dollar", "dollars", 100], ["quarter", "quarters", 25], ["dime", "dimes", 10], 
         ["nickel", "nickels", 5], ["penny", "pennies", 1]]

x = int(input("Change amount in pennies: "))
out = ""
for name, name_pl, val in coins:
    n_coin, x = divmod(x, val)
    if n_coin > 0:
        if len(out) > 0: out += "\n"
        if n_coin == 1:
            out += "1 " + name
        else:
            out += str(n_coin) + " " + name_pl
if len(out) > 0:
    print(out)
else:
    print("No change")

如果你碰巧好奇贪婪算法何时能够给出最少的硬币,何时不能给出最少的硬币,我讨论了关于数学.stackexchange

It seems simpler to add to change as you go, rather than removing anything, particularly as its initial assignment there really shouldn't work and retains no connection to the variables that you use to compose change.

So for example

    change = ``

    if num_of_dollars > 1:
        dollars = str(num_of_dollars)+' Dollars\n'
    elif num_of_dollars == 1:
        dollars = str(num_of_dollars)+' Dollar\n'
    if num_of_dollars > 0:
        change += dollars
    

etc - I'm trying to keep your structure as much as possible.

:
:

But for when you know about lists, and lists of lists:

coins = [["dollar", "dollars", 100], ["quarter", "quarters", 25], ["dime", "dimes", 10], 
         ["nickel", "nickels", 5], ["penny", "pennies", 1]]

x = int(input("Change amount in pennies: "))
out = ""
for name, name_pl, val in coins:
    n_coin, x = divmod(x, val)
    if n_coin > 0:
        if len(out) > 0: out += "\n"
        if n_coin == 1:
            out += "1 " + name
        else:
            out += str(n_coin) + " " + name_pl
if len(out) > 0:
    print(out)
else:
    print("No change")

And if you happen to be curious about when a greedy algorithm works to give the fewest coins and when it doesn't, I discussed that on maths.stackexchange

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文