Python中的精确更改

2022-01-30 18:57:32 标签 pythonif-statementinput

我正在尝试用Python写代码,在那里它使用最少的硬币和每行一个硬币类型输出精确的变化。硬币类型有美元、25美分、10美分、5美分和便士。我还必须使用单数和复数的硬币名称,如1便士和2便士。当我输入45并运行代码时,我得到一个错误说(您的程序没有产生输出)。下面是我的代码:

total_change = int(input())
if total_change <= 0:
print('No change')
if total_change >= 100:
dollar = total_change//100
dollar_change = total_change % 100
if dollar == 1:
    print(dollar + ' Dollar')
elif dollar > 1:
    print(dollar + ' Dollars')
    
elif dollar_change >= 25:
    quarter = dollar_change//25
    quarter_change = dollar_change % 25
    if quarter == 1:
        print(quarter + ' Quarter')
    elif quarter > 1:
        print(quarter + ' Quarters')
        
    elif quarter_change >= 10:
        dime = quarter_change // 10
        dime_change = quarter_change % 10
        if dime == 1:
            print(dime + ' Dime')
        elif dime > 1:
            print(dime + ' Dimes')
            
        elif dime_change >= 5:
            nickel = dime_change // 5
            nickel_change = dime_change % 5
            if nickel == 1:
                print(nickel + ' Nickel')
            elif nickel > 1:
                print(nickel + ' Nickels')
                
        elif nickel_change >= 1:
                penny = nickel_change // 1
                if penny == 1:
                    print(penny + ' Penny')
                else:
                    print(penny + ' Pennies')
                
                

###你的代码有许多问题需要解决,包括缺乏输入值0 <的条件;total_change < 100 problems with indentation (the elif blocks should be aligned) unnecessary variables (you do not need& lt;100个关于缩进的问题elif blocks should be aligned) unnecessary va块应该对齐)不必要的变量(你不需要像nickel_change and dime_change - total_change is all that matters) an和dime_change - total_change is是所有的重要)和你试图打印dollar + ' Dollar' even though dollar was a numeri尽管dollar was a numeri是一个数值变量。

我想改进你的代码中的问题,使它功能良好,但不完全重写你的工作。所以我要提供的代码的基本框架是相同的。

我用了递归的方法。我有以下函数与所有你的(清洁)代码:

def printCurrency(total_change):
    dollar = total_change//100
    dollar_change = total_change % 100
    if dollar == 1:
        print(str(dollar) + ' Dollar')
        printCurrency(total_change-1*100)
    elif dollar > 1:
        print(str(dollar) + ' Dollars')
        printCurrency(total_change-dollar*100)
    elif dollar_change >= 25:
        quarter = dollar_change//25
        quarter_change = dollar_change % 25
        if quarter == 1:
            print(str(quarter) + ' Quarter')
            printCurrency(total_change-1*25)
        elif quarter > 1:
            print(str(quarter) + ' Quarters')
            printCurrency(total_change-quarter*25)
    elif dollar_change >= 10:
        dime = dollar_change // 10
        dime_change = dollar_change % 10
        if dime == 1:
            print(str(dime) + ' Dime')
            printCurrency(total_change-1*10)
        elif dime > 1:
            print(str(dime) + ' Dimes')
            printCurrency(total_change-dime*10)
    elif dollar_change >= 5:
        nickel = dollar_change // 5
        nickel_change = dollar_change % 5
        if nickel == 1:
            print(str(nickel) + ' Nickel')
            printCurrency(total_change-1*5)
        elif nickel > 1:
            print(str(nickel) + ' Nickels')
            printCurrency(total_change-nickel*5)
    elif dollar_change >= 1:
            penny = dollar_change // 1
            if penny == 1:
                print(str(penny) + ' Penny')
                printCurrency(total_change-1*1)
            else:
                print(str(penny) + ' Pennies')
                printCurrency(total_change-penny*1)

请注意,每次打印一行时,函数都会再次运行,但在减去我们已经处理过的更改之后。

几个例子:

>>> printCurrency(45)
1 Quarter
2 Dimes
>>> printCurrency(101)
1 Dollar
1 Penny
>>> printCurrency(349)
3 Dollars
1 Quarter
2 Dimes
4 Pennies

将其与输入绑定到原始框架中……

total_change = int(input())
if total_change <= 0:
    print('No change')
if total_change >= 0:
    printCurrency(total_change)

如果您对我对您的代码所做的更改有任何问题,请告诉我!

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码