Hi there, newbie to python and in need of help for an assignment.

I have to write a Python program that prompts the user to input the before-tax price of an item and
the payment from the customer. It should then perform two tasks. First, compute and display the
after-tax price, assuming a tax rate of 5%. Second, compute the change owed to the customer, and break that down
into dollars, quarters, dimes and pennies, with the total number of coins in the breakdown minimized. Display such breakdown information.Note, if the after-tax price includes a certain fraction of a penny, ignore that fraction.

Thanks for the help!

Dani AI

Generated

A few focused points before diving in: treat money as integer cents (avoid float rounding), floor any fractional penny in the after-tax total per the assignment, and remember that greedy coin-taking (largest-first) can fail when the allowed coins are only dollars, quarters, dimes and pennies. showed that greedy/naive counts can be non‑optimal; 's divmod idea is neat when greedy is valid; is right that effort is expected — the code below is compact, robust, and works for the exact constraints you described.

from decimal import Decimal, ROUND_DOWN

def to_cents(s):
    d = Decimal(s)
    d = d.quantize(Decimal('0.01'), rounding=ROUND_DOWN)   # drop any fraction of a penny
    return int(d * 100)

def min_coins(amount, coins=(100, 25, 10, 1)):
    INF = 10**9
    dp = [INF] * (amount + 1)
    pick = [-1] * (amount + 1)
    dp[0] = 0
    for c in coins:
        for v in range(c, amount + 1):
            if dp[v - c] + 1 < dp[v]:
                dp[v] = dp[v - c] + 1
                pick[v] = c
    counts = {c: 0 for c in coins}
    v = amount
    while v > 0:
        coin = pick[v]
        counts[coin] += 1
        v -= coin
    return counts

# usage: parse inputs, compute after-tax cents with integer math, then call min_coins()
# after_cents = (before_cents * 105) // 100  # floor fractional penny as required

Troubleshooting notes: validate that payment >= after-tax amount and handle the insufficient-payment case. Use Python 3 (input returns strings) and Decimal parsing above to avoid float surprises. If you prefer a simpler brute-force search, already sketched that; the DP shown here is deterministic, fast for typical cash amounts, and guarantees the minimum number of coins under the assignment's denomination set.

Recommended Answers

All 7 Replies

Just for to start with smth...

x = input('Enter the before-tax price (in cents):')
y = input('Enter your painment (in cents):')
z = y - int(1.05 * x)
print 'Your change is:'
print '$:', z / 100
z = z % 100
print 'quarters:', z / 25
z = z % 25
print 'dimes:', z / 10
z = z % 10
print 'pennies:', z

PS
It does not minimize answers.
PPS
Maybe the penny = 5 cents? I assumed it is = 1 cent.

commented: might accidentally a nickel? +3

hotblink (Christopher Dela Paz) you should give at least an attempt to code this yourself. Most of us frown on doing homework for you.

FWIW, you can also use divmod instead of two statements (divide and mod), but I doubt that this code would be accepted from a beginning student, and only prints the plural form of the words.

x = input('Enter the before-tax price (in cents): ')
y = input('Enter your painment (in cents): ')
z = y - int(1.05 * x)
print 'Your change is: ', z
for divisor, lit in [(100, "Dollars"), (25, "Quarters"),
                      (10, "Dimes"), (5, "Nickels"), (1, "Pennies")]:
    whole, z = divmod(z, divisor)
    print "%9s = %d" % (lit, whole)

hotblink (Christopher Dela Paz) you should give at least an attempt to code this yourself. Most of us frown on doing homework for you.

+1

I think the nickels are missed here on purpose. E.g.,
80 = 3 * 25 + 5 * 1 => 8 coins
80 = 2 * 25 + 3 * 10 => 5 coins (required minimum)

x = input('Enter the before-tax price (in cents): ')
y = input('Enter your painment (in cents): ')
z = y - int(1.05 * x)

ans = [0, 0, 0, z]
a = [z / 100, 0, 0, 0]
i = a[0]
for j in range(0, (z - i * 100) / 25 + 1):
    a[1] = j
    for k in range(0, (z - i * 100 - j * 25) / 10 + 1):
        a[2] = k
        a[3] = z - i * 100 - j * 25 - k * 10
        if sum(a) < sum(ans):
            ans = a[:]

print 'Your change is:', z
print 'Dollars =', a[0]
print 'Quarters =', a[1]
print 'Dimes =', a[2]
print 'Pennies =', a[3]


Enter the before-tax price (in cents): 3456
Enter your painment (in cents): 56789
Your change is: 53161
Dollars = 531
Quarters = 2
Dimes = 1
Pennies = 1

Cute, a payment can be a painment.

Cute, a payment can be a painment.

heh You are right!
Plus, I slipped typos at the very end of my code (should be "ans[]", not "a[]").

x = input('Enter the before-tax price (in cents): ')
y = input('Enter your painment (in cents): ')
z = y - int(1.05 * x)

ans = [0, 0, 0, z]
a = [z / 100, 0, 0, 0]
i = a[0]
for j in range(0, (z - i * 100) / 25 + 1):
    a[1] = j
    a[2] = (z - i * 100 - j * 25) / 10
    k = a[2]
    a[3] = z - i * 100 - j * 25 - k * 10
    if sum(a) < sum(ans):
        ans = a[:]

print 'Your change is:', z
print 'Dollars =', ans[0]
print 'Quarters =', ans[1]
print 'Dimes =', ans[2]
print 'Pennies =', ans[3]

###########################################

Enter the before-tax price (in cents): 3438
Enter your painment (in cents): 56789
Your change is: 53180
Dollars = 531
Quarters = 2
Dimes = 3
Pennies = 0
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.