## Change Counter Help

This is the place for queries that don't fit in any of the other categories.

### Change Counter Help

I have to write a program that calculates change for school and I am stuck. Here's the assignment:

Create a program that calculates change after a purchase. This will be a sequential algorithm, which is one of the simplest programming patterns. Your solution requires no branching or looping. Simply write a series of statements that will execute in order.

The program should ask for a purchase price and the amount of cash tendered. It should then determine how many of the following denomininations should be returned:
penny
\$0.01
nickel
\$0.05
dime
\$0.10
quarter
\$0.25
dollar
\$1.00
five
\$5.00
ten
\$10.00
twenty
\$20.00

Sample Run

Price of the item:
21.37
Cash tendered:
50.00
Change: 28.63
Change Left: 2863
twenties: 1
tens: 0
fives: 1
ones: 3
quarters: 2
dimes: 1
nickles: 0
pennies: 3

So far I have:
Code: Select all
`# ask user price of the item they want to purchasevarPrice = raw_input("Price of the item: ")# ask user amount of cash tenderedvarCashPaid = raw_input("Cash tendered: ")# calculate and state changevarChange = float(varCashPaid) - float(varPrice)float(varChange)print "Change: ", varChange# convert change to penniesvarChangeLeft = float(varChange) * 100print "Change Left: ",varChangeLeft`

How do I get further? I am having trouble figuring out how to determine the amount of each denomination of change that should be returned.
Last edited by stranac on Fri Oct 04, 2013 4:29 pm, edited 1 time in total.
Reason: First post lock
samkrzy

Posts: 3
Joined: Fri Oct 04, 2013 1:51 pm

### Re: Change Counter Help

You first find out how many of the largest denomination you can use, then do that again for the remainder and the next denomination, and repeat a few more times.
You can use division and modulo for these, or use the divmod() function.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Change Counter Help

update:
Code: Select all
`# ask user price of the item they want to purchasevarPrice = raw_input("Price of the item: ")# ask user amount of cash tenderedvarCashPaid = raw_input("Cash tendered: ")# calculate and state changevarChange = float(varCashPaid) - float(varPrice)float(varChange)print "Change: ", varChange# convert change to penniesvarChangeLeft = (varChange) * 100print "Change Left: ",int(varChangeLeft)# calculate number of twenty dollar bills are in changevarTwenties = int(varChangeLeft) / 2000print "twenties: ",(varTwenties)# calculate number of tens in change...`

So I figured out the twenties, but after that I'm stuck.
samkrzy

Posts: 3
Joined: Fri Oct 04, 2013 1:51 pm

### Re: Change Counter Help

This might help:
Code: Select all
`# get the remainder after taking out the 20 dollar billsvarChangeLeft = varChangeLeft % 2000`
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Change Counter Help

I think I figured it out. Thanks!
samkrzy

Posts: 3
Joined: Fri Oct 04, 2013 1:51 pm

### Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 5 guests