I need help!

The Python program I need to write accepts the price of an item, the amount paid for it, and then calculates the change in the smallest number of bills/coins. I'm so lost!

Can anyone help?

Thanks!

The Python program I need to write accepts the price of an item, the amount paid for it

Start by asking and receiving from the keyboard, the price and the amount paid. Yes, we can help you with code you've written, but won't do it for you.

You could write down some amounts from your head and the amount of change the program should suggest to return. (One your shopping receipts plus some imaginary payments for each amount in it, for example) Put those as test function unittest and run that with your function for change to test if it passes the tests.

It would be little difficult to keep track of all coins that actually exist in cash box after actions, so you maybe can assume infinite amount of change.

To start with here one test case:

Price 4.35, payment 5.
First returned coin 25c=quarter, why? What are the other coins? Why?

There are still 50 cent pieces but you don't see them much, so the first thing is to find out what coins are acceptable.

I worked it out. I had some trouble working it out to the penny, but I fixed that with the "+0.01" part. I'm curious, Would any of you have done this differently?


# ========================================= main
def main():

purchamt, payamt = inputdata()
change, dollars, quarters, dimes, nickels, pennies = calcchange(purchamt, payamt)
printresults (change, dollars, quarters, dimes, nickels, pennies)
return
# ========================================= input data
def inputdata():
purchamt=float(raw_input("Enter the cost of the item purchased: "))
payamt=float(raw_input("Enter the amount paid for the item: "))
return purchamt, payamt


# ========================================= calculate the change
def calcchange(purchamt, payamt):
change = payamt - purchamt

cents = change*100+0.01

print cents
dollars = int(cents /100)
cents = cents %100
quarters = int(cents/25)
cents = cents %25
dimes = int(cents/10)
cents = cents %10
nickels = int(cents/5)
cents = cents %5
pennies = int(cents)

return change, dollars, quarters, dimes, nickels, pennies


# ========================================= print results
def printresults(change, dollars, quarters, dimes, nickels, pennies):

print
print "Amount of change to give back: ", change
print 'dollars: $', dollars
print 'quarters: ', quarters
print 'dimes: ', dimes
print 'nickels: ', nickels
print 'pennies: ', pennies


return

# ===================================== call the main

main()

I would have posted it with (CODE) tags ;)

Maybe divmod would have been little cleaner for the divisor operations. I do not know what is your cent trouble, I check it later when I am not so busy as now.
using:

# ========================================= main

style comments is not encouraged in Python.
Instead it would be usefull to put some docstring comments for each functions

def main():
    purchamt, payamt = inputdata()
    change, dollars, quarters, dimes, nickels, pennies = calcchange(purchamt, payamt)
    printresults (change, dollars, quarters, dimes, nickels, pennies)
    return
# ========================================= input data
def inputdata():
    purchamt=float(raw_input("Enter the cost of the item purchased: "))
    payamt=float(raw_input("Enter the amount paid for the item: "))
    return purchamt, payamt


# ========================================= calculate the change
def calcchange(purchamt, payamt):
    change = payamt - purchamt

    cents = change*100+0.01

    print cents
    dollars = int(cents /100)
    cents = cents %100
    quarters = int(cents/25)
    cents = cents %25
    dimes = int(cents/10)
    cents = cents %10
    nickels = int(cents/5)
    cents = cents %5
    pennies = int(cents)

    return change, dollars, quarters, dimes, nickels, pennies




# ========================================= print results
def printresults(change, dollars, quarters, dimes, nickels, pennies):

    print
    print "Amount of change to give back: ", change
    print 'dollars: $', dollars
    print 'quarters: ', quarters
    print 'dimes: ', dimes
    print 'nickels: ', nickels
    print 'pennies: ', pennies


    return

# ===================================== call the main

main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.