开发者

if/elif/else statement help with money

开发者 https://www.devze.com 2023-04-06 05:20 出处:网络
Updated my new code at the bottom of the page as an answer. So for my CS 170 class, we have to make a program that has user input for less than $10 and change is returned in the least amount of coins

Updated my new code at the bottom of the page as an answer.

So for my CS 170 class, we have to make a program that has user input for less than $10 and change is returned in the least amount of coins, no bills or 50 cent pieces. For the most part the program does well except when you run into a 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.
>>> 

the program completely skips over the dimes section and straight to nickels providing 4 as a solution when the least amount should be 8 quarters, 2 dimes and end. Also I'm not really skilled in loops too much, but I know this would be possible and a lot shorter code, and cleaning up the code advice would be nice as well. Thanks for any help!

# 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
  开发者_Go百科  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


There are a few changes you could make to clean up this code, and one of them might fix your problem. First, pass does absolutely nothing. It is usually used as a placeholder for a loop or function that will be filled in later. Also, the conditions of your elif statements are mutually exclusive with the if statements they follow, so

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)

can be rewritten as

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)

for each if/elif statement. Also, in the statement

if change >=.01 <=.04:

you are testing whether

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

To make it do what you want, the statement should be rewritten as

if .01 <= change <= .04

In addition, you are using floating point numbers, which often lead to rounding errors. To avoid these errors, I would suggest either representing your money as an integer number of cents and multiply all of the numbers in your problem by 100 or use something with fixed point numbers like python's decimal module.


This is not doing what you expect:

elif change >= .10 <= .24:

It looks like you intend something like:

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

or Python also supports:

elif .10 <= change <= .24:

However, you will next run into floating point rounding problems of various kinds. I suggest you first convert the input number to an integer number of cents, and perform all your calculations in cents. Avoid floating point numbers when dealing with money.


So I got it worked out in a better format with cleaner print code. Thanks for the help guys! If anyone wants to know the difference between the 2 codes, it's getting it out of floating point like the others suggested and converting what is needed to integers, multiplying the integers by the specific amount, say a quarter, and then subtracting int * coin/bill from the change. Worked out well. I tried experimenting with a for statement, but that didn't turn out too well since I don't know a lot about it yet either. Until next time...

Again thanks guys!

Here's the finished code for anyone wondering about it:

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!"
0

精彩评论

暂无评论...
验证码 换一张
取 消