Not Yet Answered # Calculate the change

woooee 813 pyTony 888 woooee 813 Discussion Starter tk-421 pyTony 888 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

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.

0

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?

*Edited 6 Years Ago by pyTony*: n/a

0

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.

0

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()

0

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()
```

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...