如果/ elif / else声明帮助钱

时间:2011-09-20 04:00:48

标签: python if-statement

在页面底部更新了我的新代码作为答案。

因此,对于我的CS 170课程,我们必须制作一个用户输入少于10美元的程序,并以最少的硬币,没有账单或50美分返回更改。在大多数情况下,程序运行良好,除非您遇到x.x0 e.g:

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Amount due:
 7.80
Amount in return
 2.20.
Quaters in return 8.
Dimes in return 0.
Nickels in return 4.
>>> 

该程序完全跳过角钱部分并直接切换到镍,提供4作为解决方案,当最少量应该是8个季度,2角钱和结束。另外,我不太熟悉循环,但我知道这是可能的,而且代码更短,清理代码建议也很好。谢谢你的帮助!

# optional.py
# Calculating the least amount of change in return for a $10 bill.

## amount due
due = input("Amount due:\n ")
## if amount is more than 10, exit program
if due > 10.00:
    print "Please enter a number lower then 10.00."
    exit()
## if amount is less than 0, exit program
if due < 0:
    print "Please enter a number greater than 0.00."
    exit()
## subtract amount from 10
else:
    change = 10.00 - due
    print "Amount in return\n %0.2f." % change
## if amount is 0, no change
if change == 0:
    print "No change in return."
## passes expression if previous not met
    pass
elif change >= .25:
## setting q, dividing change by .25
    q = change / .25
## maaking q an integer
    quaters = int(q)
    print "Quaters in return %r." % quaters
## subtracting quaters from chane
    change = change - (quaters *.25)

if change < .10:
    pass
elif change >= .10 <= .24:
    d = change * .1
    dimes = int(d)
    print "Dimes in return %r." % dimes
    change = change - (dimes * .1)

if change < .05:
    pass
elif change >=.05 <=.09:
    n = change / .05
    nickels = int(n)
    print "Nickels in return %r." % nickels
    change = change - (nickels * .05)

if change == .01:
    pennies = change / .01
    print "Pennies in return %r." % pennies
elif change >=.01 <=.04:
    p = change / .01
    print "Pennies in return %0.0f." % p

3 个答案:

答案 0 :(得分:3)

您可以对清理此代码进行一些更改,其中一个可能会解决您的问题。首先,pass绝对没有任何作用。它通常用作循环或函数的占位符,稍后将填充。此外,您elif语句的条件与其关注的if语句互斥,因此

if change == 0:
    print "No change in return."
## passes expression if previous not met
    pass
elif change >= .25:
## setting q, dividing change by .25
    q = change / .25
## maaking q an integer
    quaters = int(q)
    print "Quaters in return %r." % quaters
## subtracting quaters from chane
    change = change - (quaters *.25)

可以改写为

if change >= .25:
## setting q, dividing change by .25
    q = change / .25
## making q an integer
    quaters = int(q)
    print "Quaters in return %r." % quaters
## subtracting quaters from change
    change = change - (quaters *.25)
每个if / elif语句的

。另外,在声明中

if change >=.01 <=.04:

您正在测试是否

change >= .01 and .01 <= .04

为了使它做你想做的事,该语句应该重写为

if .01 <= change <= .04

此外,您使用的是floating point numbers,这通常会导致舍入错误。为了避免这些错误,我建议将您的资金表示为整数分,并将问题中的所有数字乘以100或使用定点数字python's decimal module

答案 1 :(得分:2)

这不符合您的期望:

elif change >= .10 <= .24:

看起来你打算像:

elif change >= .10 and change <= .24:

或Python也支持:

elif .10 <= change <= .24:

但是,接下来会遇到各种浮点舍入问题。我建议你先将输入数字转换为整数美分,然后以美分执行所有计算。处理钱时避免使用浮点数。

答案 2 :(得分:1)

所以我用更清晰的打印代码以更好的格式解决了这个问题。谢谢你的帮助! 如果有人想知道2个代码之间的区别,它会像其他人建议的那样将浮点数转换为整数,将整数乘以特定的数量,比如四分之一,然后减去int * coin /从改变中收取费用。工作得很好。我试着尝试一个for语句,但事实并非如此,因为我对此也不太了解。直到下一次......

再次感谢你们!

以下是任何想知道它的人的完成代码:

import sys

due = input("Please enter the amount due on the item(s):\n ")
# if over $10, exit
if due > 10:
    print "Please enter an amount lower then 10."
    sys.exit(1)
## if under/equal 0, exit
if due <= 0:
    print "Please enter an amount greater than 0."
    sys.exit(2)
## 10 - due = change, converts change into cents by * by 100 (100 pennies per dollar)
else :
    change = 1000 - (due * 100)
## if change is 0, done
    if change == 0:
        print "No change in return!"
## if not 0 makes change2 for amount in return
    else:
        change2 = change / 100
        print "Amount in return:\n $%.2f." % change2
## if change > 500, subract 500 and you get 1 $5 bill

if 500.0 <= change:
    bill_5 = change / 500
    b5 = int(bill_5)
    change = change - 500

## if change is over 100, change divided by 100 and subtracted from change for quaters

if 100.0 <= change:
    dollars = change / 100
    dollar = int(dollars)
    change = change - (dollar * 100)

if 25 <= change < 100:
    quaters = change / 25
    quater = int(quaters)
    change = change - (quater * 25)

if 10 <= change <= 24:
    dimes = change / 10
    dime = int(dimes)
    change = change - (dime * 10)

if 5 <= change < 10:
    nickels = change / 5
    nickel = int(nickels)
    change = change - (nickel * 5)

if 0 < change < 5:
    pennies = change / 1
    penny = int(pennies)
    change = change - (penny * 1)

print "Change in return:\n $5:%i\n $1:%i\n Quaters:%i\n Dimes:%i\n Nickels:%i\n Pennies:%i " % (
    b5, dollar, quater, dime, nickel, penny )


if 0 >= change:
    print "Done!"