Change Counter Help

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

Change Counter Help

Postby samkrzy » Fri Oct 04, 2013 3:42 pm

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 purchase

varPrice = raw_input("Price of the item: ")

# ask user amount of cash tendered

varCashPaid = raw_input("Cash tendered: ")

# calculate and state change

varChange = float(varCashPaid) - float(varPrice)
float(varChange)
print "Change: ", varChange

# convert change to pennies
varChangeLeft = float(varChange) * 100
print "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

Postby stranac » Fri Oct 04, 2013 4:36 pm

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.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: Change Counter Help

Postby samkrzy » Fri Oct 04, 2013 4:37 pm

update:
Code: Select all
# ask user price of the item they want to purchase

varPrice = raw_input("Price of the item: ")

# ask user amount of cash tendered

varCashPaid = raw_input("Cash tendered: ")

# calculate and state change

varChange = float(varCashPaid) - float(varPrice)
float(varChange)
print "Change: ", varChange

# convert change to pennies

varChangeLeft = (varChange) * 100
print "Change Left: ",int(varChangeLeft)

# calculate number of twenty dollar bills are in change

varTwenties = int(varChangeLeft) / 2000
print "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

Postby stranac » Fri Oct 04, 2013 5:06 pm

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

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm

Re: Change Counter Help

Postby samkrzy » Fri Oct 04, 2013 9:36 pm

I think I figured it out. Thanks!
samkrzy
 
Posts: 3
Joined: Fri Oct 04, 2013 1:51 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 3 guests