0

I'm doing a lab for school to find the exact change. For example 126 is the input the answer would be 1 dollar 1 quarter 1 penny. grammar matters too. I can't get the pennies part to work. It seems to stop working after 104. Also, I'm sure there are simpler ways to write the code, but this is all we learned so far.

money = int(input())

money = money / 100
dollar = int(money // 1)
remaining_cents = int((money - dollar) * 100)

if money <= 0:
    print('No change')
if dollar == 1:
    print(dollar, 'Dollar')
if dollar > 1:
    print(dollar, 'Dollars')
if remaining_cents >= 25:
    quarters = remaining_cents // 25
    remaining_cents = remaining_cents - (quarters * 25)
    if quarters == 1:
        print(quarters, 'Quarter')
    elif quarters > 1:
        print(quarters, 'Quarters')
if remaining_cents >= 10:
    dimes = remaining_cents // 10
    remaining_cents = remaining_cents - (dimes * 10)
    if dimes > 1:
        print(dimes, 'Dimes')
    elif dimes == 1:
        print(dimes, 'Dime')
if remaining_cents >= 5:
    nickels = remaining_cents // 5
    remaining_cents = remaining_cents - (nickels * 5)
    if nickels > 1:
        print(nickels, 'Nickels')
    elif nickels == 1:
        print(nickels, 'Nickel')
else:
    if remaining_cents > 1:
        print(remaining_cents, 'Pennies')
    elif remaining_cents == 1:
        print(remaining_cents, 'Penny')
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • With the test case of `126`, my output using your code was ""1 Dollar 1 Quarter 1 Penny"" What exactly is the problem you're having? – ZXYNINE Jan 14 '22 at 06:26
  • I'm not sure if this is the source of your problem, but`dollar = money // 100` and `remaining_cents = money % 100` avoids potential inaccuracies from converting to floating point and back to integer. – Paul Hankin Jan 14 '22 at 09:18

1 Answers1

0

if you did this for the pennies it should work. I am sure this is a bit late, I'm taking the same class now. but hopefully this helps the next person.

if remaining_cents >=1:
    pennies = remaining_cents // 1
    remaining_cents = pennies
    if pennies > 1:
        print(remaining_cents, 'Pennies')
    elif remaining_cents == 1:
        print(remaining_cents, 'Penny')