Python Lesson
Okay, let me stop and restart.
A loop, in programming, is a piece of code that will run in a loop until it spits out the information you want. So, think about it like this. You tell the program, ‘I want to know how many 6, 9, or 20 packs of Mcnuggets I need to buy, to purchase 55 Mcnuggets.” You see, this person has 11 kids, all eating 5 nuggets each, and only wants to worry about how many packs he buys. If ONLY he had a program that could just do the calculations for him!!!
Well, here it is:
def Mcnugget(n): for sixpack in range (0, n/6 + 1): for ninepack in range (0, (n - 6*sixpack)/9 + 1): nuggets = n - (6*sixpack + 9*ninepack) twentypack = nuggets/20 print sixpack, ninepack, twentypack if twentypack*20 + 6*sixpack + 9*ninepack == n: return (sixpack, ninepack, twentypack) return (None, None, None) for n in range (55, 56): print n print Mcnugget(n) |
This piece of code, by the way it’s put together, will show you the loop it goes through. When I run this piece of code (in other words, I tell the computer I want it to go through my code and hit enter), I get this information in response:
$ python Practice.py 55 0 0 2 0 1 2 0 2 1 0 3 1 0 4 0 0 5 0 0 6 0 1 0 2 1 1 2 (1, 1, 2) |
Look at the second line, it’s telling you that it’s testing the number 55. All the following lines show the computer (or interpreter) going through the different combinations of Mcnugget packs. The first line (0, 0, 2) is testing whether 2 twenty packs is equal to 55. 2 twenty packs, or 40 Mcnuggets, is not 55, so the computer will go through the code again. Each line below the 55, is grouped like this: # of six-packs, # of nine-packs, # of twenty-packs. So if you looked at the last line, you can see the computer (interpreter) returned with an answer of 1 six pack, 1 nine pack and 2 twenty-packs of Mcnuggets. With that combination, you can buy 55 Mcnuggets. This does not mean that support buying fast food, however I am currently sitting across from McDonald's and am starving, so I may make a side trip here soon...
Alright, so that may not make a ton of sense. It was one of the difficult things that I had to learn, this learning how different looping mechanisms work inside of each other. I’ll try to explain this by showing you a basic combination of ‘for’ loops.
for x in range (4): for y in range (5): print x, y |
The code is actually saying, “for each x in range (4): and for each y in range (5):”
X is every number between 0 and 3 (this is because Python is a ‘zero indexed’ language, meaning that all ranges start on zero, and go from there. In other words, if you ask for a range (4), it’s going to go from 0 to 3).
Y is then, subsequently, every number between 0 and 4. Let’s run a simple ‘for’ loop:
for x in range (4): print x |
If you did this, you would get this range of numbers printed back out at you:
0 1 2 3 |
However for the loop inside the loop you’re comparing two different ranges. Going back to the original piece of code, we see that one loop is inside the other loop (or nested).
for x in range (4): for y in range (5): print x, y |
Running this code, you get this in response:
0 0 0 1 0 2 0 3 0 4 1 0 1 1 1 2 1 3 1 4 2 0 2 1 2 2 2 3 2 4 3 0 3 1 3 2 3 3 3 4 4 0 4 1 4 2 4 3 4 4 |
Sorry for show the whole thing, but you get the idea. This concept, as simple as it seems, is really helpful once you fully understand it. Think about how much you could compare in order to get the desired combination you needed? Or any number of other things.
Anyways, there is your little python tutorial for the day (or Python Tut, whatever works).