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.' additional_amount = eval(raw_input('Additional amount you are paying: $')) amount_tendered = amount_tendered + additional_amount # 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])