简体   繁体   中英

Change Calculator with exact type of change given

the instructions are

Create a program that allows him to input a certain amount of change, and then print how how many quarters, dimes, nickels, and pennies are needed to make up the amount needed. For example, if he inputs 1.47, the program will tell that he needs 5 quarters, 2 dimes, 0 nickels, and 2 pennies.

I didn't really know how to do this but I gave it a shot. I'm really unsure of what to do.

print "Change Calclator"

quarter = .25
dime = .10
nickel = .5
penny = .1

moneygiven = raw_input("Enter how much money given: ")
citem = raw_input("How much did the item cost?: ")
moneygiven = float(moneygiven)
citem = float(citem)
moneyback = moneygiven - citem

qmb = moneyback % quarter
partialtotal = moneyback - qmb * quarter 
dmb = partialtotal / dime
dpartialtotal = partialtotal - dmb * dime
nmb = dpartialtotal / nickel
npartialtotal = dpartialtotal - nmb * nickel
pmb = npartialtotal / penny
ppartialtotal = npartialtotal - pmb * penny

print "You need %s quarters, %s dimes, %s nickels, %s pennies." % (qmb, dmb, nmb, pmb)

when run with 20 in moneygiven and 19.45 in citem it gives this

Change Calclator
Enter how much money given: 20
How much did the item cost?: 19.45
You need 2.2 quarters, 0.0 dimes, 0.0 nickels, 0.0 pennies.

Developing @jonrsharpe's comment, you should work with int variables holding pennies. The point here is that you have an integer amount of coins with float values, and you're mixing them when dividing, thus getting weird values. Also take into account that you should use the proper division operator.

Here a working version:

print "Change Calclator"

quarter = 25
dime = 10
nickel = 5
penny = 1

moneygiven = raw_input("Enter how much money given: ")
citem = raw_input("How much did the item cost?: ")
moneygiven = int(float(moneygiven) * 100)
citem = int(float(citem) * 100)
moneyback = moneygiven - citem

qmb = moneyback / quarter
partialtotal = moneyback - qmb * quarter 
dmb = partialtotal // dime
dpartialtotal = partialtotal - dmb * dime
nmb = dpartialtotal // nickel
npartialtotal = dpartialtotal - nmb * nickel
pmb = npartialtotal // penny
ppartialtotal = npartialtotal - pmb * penny

print "You need %s quarters, %s dimes, %s nickels, %s pennies." % (qmb, dmb, nmb, pmb)

You should use the // operator instead / operator. Another thing it that you put penny= .1 And that is the same of penny=0.10. You should use 0.01

// Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed.

Example:

 a=.25
.55//a = 2.0

Code Working

print "Change Calclator"

quarter = .25
dime = .10
nickel = .05
penny = .01

moneygiven = raw_input("Enter how much money given: ")
citem = raw_input("How much did the item cost?: ")
moneygiven = float(moneygiven)
citem = float(citem)
moneyback = moneygiven - citem

qmb = moneyback // quarter
partialtotal = moneyback - qmb * quarter 
dmb = partialtotal // dime
dpartialtotal = partialtotal - dmb * dime
nmb = dpartialtotal // nickel
npartialtotal = dpartialtotal - nmb * nickel
pmb = npartialtotal // penny
ppartialtotal = npartialtotal - pmb * penny

print "You need %s quarters, %s dimes, %s nickels, %s pennies." % (qmb, dmb, nmb, pmb)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM