Hi guys! I'm a beginner in Python and I am having a bit of trouble with this program. What I need to do is ask the user how much they must pay, and how much they are paying. Example: Buying groceries that cost \$10.27 and the user is paying \$11.00.

The program calculates the users change and also tells the user what coins make up said change. What I need help with is when I calculate the change how can I go about so I can print the correct coins needed to make up the change. I am unsure how to go about this, any help would be greatly appreciated.

This is the code:

``````COIN_NAMES = ['Dollars', 'Half-dollars', 'Quarters', 'Dimes', 'Nickels', 'Pennies']
COIN_VALUES = [100, 50, 25, 10, 5, 1]

NUMBER_OF_DENOMINATIONS = len(COIN_NAMES)

def to_cents(amount):
'''
Converts the given amount to cents
amount: the amount, in dollars and cents, to convert
Returns the number of cents in the amount
Example:
to_cents(1.34) is 134
'''

def change(to_pay):
'''
Calculates the number of dollars, half-dollars, quarters, dimes,
nickels and pennies requires to pay the given amount
to_pay: the amount to make change for
Returns a list of the required coins, in the order used above
Example:
change(369) is [3, 1, 0, 1, 1, 4]
'''
coin_counts = [0, 0, 0, 0, 0, 0]

return coin_counts

# Get price from user
price_of_item = eval(raw_input('Price of item: \$'))

# Get amount paid by user, being sure it is enough
amount_tendered = eval(raw_input('Amount you are paying: \$'))
while amount_tendered < price_of_item:
print 'Sorry.  That is not enough.'

# Calculate change required, and convert to cents
amount_to_return = amount_tendered - price_of_item
amount_in_cents = to_cents(amount_to_return)
# Get list of coins to make change
coin_list = change(amount_in_cents)

# Print results
print 'Your change is \$%.2f' % amount_to_return
for i in range(NUMBER_OF_DENOMINATIONS):
if coin_list[i] > 0:
print COIN_NAMES[i] + ': ' + str(coin_list[i])``````

You have two functions. None of them return anything, so they return None.
As you write, your change function is not implemented.

``````COIN_NAMES = ['Dollars', 'Half-dollars', 'Quarters', 'Dimes', 'Nickels', 'Pennies']
COIN_VALUES = [100, 50, 25, 10, 5, 1]

NUMBER_OF_DENOMINATIONS = len(COIN_NAMES)

def to_cents(amount):
'''
Converts the given amount to cents
amount: the amount, in dollars and cents, to convert
Returns the number of cents in the amount
Example:
to_cents(1.34) is 134
'''
return amount*100

def change(to_pay):
'''
Calculates the number of dollars, half-dollars, quarters, dimes,
nickels and pennies requires to pay the given amount
to_pay: the amount to make change for
Returns a list of the required coins, in the order used above
Example:
change(369) is [3, 1, 0, 1, 1, 4]
'''
coin_counts = [0, 0, 0, 0, 0, 0]
for i,coin in enumerate(COIN_VALUES):
while coin<=to_pay:
to_pay-=coin
coin_counts[i]+=1
return coin_counts

# Get price from user
price_of_item = eval(raw_input('Price of item: \$'))

# Get amount paid by user, being sure it is enough
amount_tendered = eval(raw_input('Amount you are paying: \$'))
while amount_tendered < price_of_item:
print 'Sorry.  That is not enough.'

# Calculate change required, and convert to cents
amount_to_return = amount_tendered - price_of_item
amount_in_cents = to_cents(amount_to_return)
# Get list of coins to make change
coin_list = change(amount_in_cents)

# Print results
print 'Your change is \$%.2f' % amount_to_return
for i in range(NUMBER_OF_DENOMINATIONS):
if coin_list[i] > 0:
print COIN_NAMES[i] + ': ' + str(coin_list[i])``````

If you are interested in a more "pythonic" solution, let me know.

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.