1

I am in a beginner scripting class where we need to write some code that makes change based on integer input. It also needs to print the correct plurality for each coin type. I am having some trouble getting my code to work, as it stops at the first operation it performs and I'm not sure how to get it to continue. I'm guessing it has something to do with how I've laid out all these elif statements and I'm not sure what the proper thing to do is to get the code to continue if it still has a remainder for the change:

# Get input for amount of change
change = int(input())

# If change is less than 1, print 'No change'
if change < 1:
    print('No change')

# If change is over 199, subtract and print 'X Dollars'
elif change > 199:
    print(change // 100, 'Dollars\n')
    change = change % 100

# If change is 100-199, subtract and print '1 Dollar'
elif 99 < change <= 199:
    print('1 Dollar\n')
    change = change % 100

# If remaining change is 50-99, subtract and print 'X Quarters'
elif 49 < change <= 99:
    print(change // 25, 'Quarters\n')
    change = change % 25

# If remaining change is 25-49, subtract and print '1 Quarter'
elif 24 < change <= 49:
    print('1 Quarter\n')
    change = change % 25

# If remaining change is 25, print '1 Quarter' and set change to 0
elif change == 25:
    print('1 Quarter')
    change = 0

# If remaining change is 20-24, subtract and print '2 Dimes'
elif 19 < change <= 24:
    print('2 Dimes\n')
    change = change % 10

# If remaining change is 10, print '1 Dime' and set change to 0
elif change == 10:
    print('1 Dime')
    change = 0

# If remaining change is 5-9, subtract and print '1 Nickel'
elif 4 < change <= 9:
    print('1 Nickel\n')
    change = change % 5

# If remaining change is 2-4, subtract and print 'X Pennies'
elif 1 < change <= 4:
    print(change // 1, 'Pennies')
    change = change % 1

# If remaining change is 1, print '1 Penny' and set change to 0
elif change == 1:
    print('1 Penny')
    change = 0

Could anyone give me an idea? Thank you for your time.

  • You're using `elif` - so only a single branch of the whole thing will execute. Just use `if` everywhere instead. You need all applicable blocks to execute. – rdas Mar 21 '21 at 16:34
  • Please read about how to create a [mre]. It will help us help you by having a more focused question, but more importantly, it will help you narrow down your code and find out where exactly is the problem. Sometimes, along the way of creating one, you will already solve the problem by yourself and wouldn't need to ask here. Also read about [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). You can also use [Python-Tutor](http://www.pythontutor.com/visualize.html#mode=edit) which helps to visualize the execution of the code step-by-step. – Tomerikoo Mar 21 '21 at 16:58
  • Thank you very much, you're a lifesaver. – NarsheDweller13 Mar 21 '21 at 17:11

2 Answers2

0

Always subtract what you have already matched from the rest of the change. And then continue using IF not ELIF, since elif will never match again once a previous if matched.

# Get input for amount of change
change = int(input())

# If change is less than 1, print 'No change'
if change < 1:
    print('No change')

# If change is over 199, subtract and print 'X Dollars'
if change > 199:
    print(change // 100, 'Dollars\n')
    change = change - change // 100 * 100

# If change is 100-199, subtract and print '1 Dollar'
if 99 < change <= 199:
    print('1 Dollar\n')
    change = change - 100

# If remaining change is 50-99, subtract and print 'X Quarters'
if 49 < change <= 99:
    print(change // 25, 'Quarters\n')
    change = change - change // 25 * 25
# ....

and so on...

Mirronelli
  • 740
  • 5
  • 14
0

If I get your question right, it's to print dollars/dollar [and sub value(s)] based on the 'change' which keeps reducing until it's zero.

Let me clarify (not a dollar user :):

  • 1 Penney = 1 Cent
  • 1 Nickel = 5 Cents
  • 1 Dime = 10 Cents
  • 1 Quarter = 25 Cents
  • 1 Dollar = 100 Cents

So if the input is say 1451 Cents the expected output is: 1451 = 14 Dollars 2 Quarters 0 Dime 0 Nickel 1 Penny

You can do something like this:

change = int(input())
wt = {'Dollar': 0, 'Quarter': 0, 'Dime': 0, 'Nickel': 0, 'Penny': 0} # weight

wt['Dollar'] = change // 100
change = change % 100
wt['Quarter'] = change // 25
change = change % 25
wt['Dime'] = change // 10
change = change % 10
wt['Nickel'] = change // 5
change = change % 5
wt['Penny'] = change // 1
change = change % 1

for key, value in wt.items():
    print(f'{value} {key}s' if value > 1 else f'{value} {key}', end=' ')
print()

Output:

➜ python file.py 
112341
1123 Dollars 1 Quarter 1 Dime 1 Nickel 1 Penny 


➜ python file.py 
24
0 Dollar 0 Quarter 2 Dimes 0 Nickel 4 Pennys # you might wanna add some small condition to rectify this :)


➜ python file.py 
521
5 Dollars 0 Quarter 2 Dimes 0 Nickel 1 Penny