Help with Python coding exercise

I’m a beginner following a free Python course on EDX and I’m having trouble with one exercise.

It should be noted that although the class can be followed for free as I am, there is a paid version of it which opens lots more of exercise pages and I did get a hint that some additional topics are learned through them so my problem is…I don’t know if the exercise I’m trying to solve can be resolved with only the knowledge I’ve acquired with free portion of the course?

Therefore, my question is, can you solve this using only variables, math, logical and boolean operators and if absolutely necessary, if statements?
Thanks!

amount = 17

#The variable above describes an amount of money measured in
#dollars. Imagine you want to select the bills (1-dollar bills,
#5-dollar bills, 10-dollar bills, etc.) that make up that
#amount of money. For example, 17 dollars is one $10, one $5,
#and two $1s.
#
#Write a program that will print out the bills needed to
#arrive at the amount shown above. Assume that we always want
#the maximum number of large bills: for example, for 17 dollars,
#we want one $10, one $5, and two $1s, not three $5s and two $1s.
#You may assume that the largest bill you have on hand is a
#$50-dollar bill.
#
#Your code should print the following (with the correct numbers
#based on the value of amount):
#
#Fifties: 0
#Twenties: 0
#Tens: 1
#Fives: 1
#Ones: 2

As you’ll have guessed, I’m a complete beginner.
So this is as far as I was able to get:

# create a variable for each bill starting with a value = 0
Fifties = 0
Twenties = 0
Tens = 0
Fives = 0
Ones = 0

# figure out how many bills of each can fit in the initial amount 
Fifties = amount // 50
Twenties = amount // 20
Tens = amount // 10
Fives = amount // 5
Ones = amount // 1

# print out the results as requested by the exercise (although this is not yet the correct answer of course)
print("Fifties:", Fifties)
print("Twenties:", Twenties)
print("Tens:", Tens)
print("Fives:", Fives)
print("Ones:", Ones)

So that gives me the number of bills of each type that can fit in the given amount but then comes the challenge of telling the program that it must use the largest bills first…any hints please?
I get a feeling that the % operator could be useful as well but can quite fit in?

Thanks for your reply sanity!
Glad to hear I’m on the right track :slightly_smiling_face:
As for the rest…well, that’s why at the end of my OP I was asking for some hints! :wink:
So if you can edge me in the right direction, it would be much appreciated. Maybe tell me which operators to use to tell the code to use the larger bills firsts?
Was my instinct right that % operator could come into play here as well?

As for the snake_case, duly noted! (although I seem to remember reading somewhere that the convention was rather camel_Case?)

Python doesn’t have === or !== operator.

Using // to calculate specific number of bills is a good start. However you will need to make some additional operations. Notice that now you are calculating how many bills you’d need, but without regard if some higher bill was used already and you have lower amount to cover.

On the another note - you should name variables lowercase in the so-called snake_case, as that’s what python’s convention is for naming variables.

1 Like

As @sanity said, you are working in Python, so just ignore the Javascript advice you got.

All of the lines that you have are good, you just need to add some extra logic.

In this case, you can think about how you would physically do this task. If you have the amount 17 and you decide to use a single 10, then what amount do you still need to provide bills for? How can you decrease the amount variable? A % might help, but if it’s more natural, think about how you could remove a single, or multiple, 10 dollar bills from the amount.

Variable naming conventions:
snake_case
camelCase
PascalCase

Thanks BobSmith!
Well explained, I see your point…I’ll give it a try :slightly_smiling_face:

So all three naming conventions are considered equally fine for Python? I’m at a point where no habit has set in so I’ll gladly go with the most practiced and agreed upon.

Here is the direct info from the Python style guide (called PEP 8):

Function and Variable Names
Function names should be lowercase, with words separated by underscores as necessary to improve readability.
Variable names follow the same convention as function names.
mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.

So, Python prefers snake_case.

Ok, thx! Will do…
Having more trouble than I expected with the exercise…if you could offer a bit more help?
I just can’t figure out how to tell the program to select the right number of the biggest bills?
I could probably venture into a very long, complicated and sloppy code that might (or not?) get the job done but I would rather have a hint to work in the right direction with a more elegant and realistic code.

Can you really solve this with only the tools I mentioned in my OP?


# figure out how many bills of each can fit in the initial amount 
Fifties = amount // 50
Twenties = amount // 20
Tens = amount // 10
Fives = amount // 5
Ones = amount // 1

this will get you pretty far… if you can update what amount is after each line.

That’s the thing…I can’t really figure that part out.
I’m guessing that, with enough effort, anyone can learn a coding language but acquiring that coder’s logic mindset takes a lot of practice so I’m not there yet…at all!

What I figured out is, I will always want the largest bill in it’s largest quantity so whatever non-zero value that comes up first starting with the ‘Fifties’ will be included in the correct answer. And then the ‘Ones’ will always fill out the remainder that could not be provided by any other bill.
But what happens with the bills in between…I can’t figure out :confused:

The problem becomes more apparent if you take another amount. Let’s say amount = 46
Then my code would tell me:

amount = 46

fifties: 0
twenties: 2
tens: 4
fives: 9
ones: 46

As I said previously, fine, I know for sure that…

fifties = 0
twenties = 2 

…are correct. But how do I tell the program to assign…

tens = 0
fives= 1

(I hope…i think I can figure out how to assign the remainder to the ‘ones’)

Once you have twenties = 2, what is the remaining amount that has not been assigned to bills? 6, right? How did I compute that number? How can you adjust amount?

I think we are coursemates, I am in GTx: CS1301xI and have the same problem. This topic helped me to solve it. I used the BobSimth’s advice and after each “print” I just upgraded the value of amount with %.
fift = amount//50
print(“Fifties:”,fift)
amount = amount%50

1 Like

amount = 17

fifties= int(amount/50)

twenties= int(amount/20)

tens=int(amount/10)

fives= int((amount%10)/5)

ones= int(((amount%10)%5)/1)

print('fifties= ',fifties)

print('twenties= ', twenties)

print('tens= ',tens)

print('fives= ',fives)

print('ones= ',ones)

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.