First off, range(161,-1)
won't iterate on any items because the first argument start
is greater than the second argument end
. Just do range(161)
.
Another thing is, you're not using your variables consistent with the units they are meant to represent. I'll explain while stepping through your script:
for tens in range(161):
Here, you're attempting different number of ten dollar bills - from 0 to 160. Looking good (now that we've stopped iterating on nothing).
fifty = (160 - 10*tens)/50
Now, this doesn't make sense. 10*tens is the dollar value of how many tens you are trying. why are you subtracting that from 160 -the total number of bills? These are two completely unlike units and it makes no sense to subtract them. Instead, just subtract the number of tens from the total number of bills to get a number of fifties with fifty = 160 - tens
if 10*tens + 50*fifty == 1760:
print(tens,fifty)
Assuming tens and fifty means the number of each type of bill - which the previous point covered - this is fine.
So, altogether, your code will print the correct values of tens & fifty if it looks like this:
for tens in range(161):
fifty = 160 - tens
if 10*tens + 50*fifty == 1760:
print(tens,fifty)
The solution uses break
because doing that quits the loop so that the values of tens
and fifty
are correct and the rest of the program can use them. As the above code is, fifty
and tens
would continue to change even after a solution is found. Using break
is a straightforward way of keeping the values after you've found a solution.