My bike is still chained to the rail on my balcony. If you recall, I said previously that I believed the combination to the lock had a 1 and a 4 in it. The program I wrote gave a list of all the possible combinations, and it turned out there were 1,296 possibilities. That’s a lot of numbers to fiddle around with on a combination lock. So I figured I should write a new program, this time only showing combinations that had both a 1 and a 4 in them. Surely that would cut out a lot of unnecessary combos, right? I got to work, and here is what I came up with:
x = 0
for a in range (0, 6):
for b in range (0, 6):
for c in range (0, 6):
for d in range (0, 6):
combo = str(a+1) + str(b+1) + str(c+1) + str(d+1)
if "1" in combo and "4" in combo:
print combo
x = x + 1
print "There are", x, "combinations that contain both a 1 and a 4."How it works
We start off by assigning the value 0 to the variable x, which will keep count of the number of combos with both a 1 and a 4 in them. We make four loops, a, b, c and d, to create all the different possible combinations. inside the fourth loop, we make a string called combo and put the string representations of the variables a, b, c and d in it. Then we look to see if the string combo has both “1″ and “4″ in it. If it does, we print combo and add 1 to the value of x. Once the program runs through all its loops, we have the program tell us how many combinations contain both a 1 and a 4. There are 302 of them. That’s a lot less than 1296, and it seems manageable.
Why the loops go from 0 to 6
When setting up a loop in Python, the number you want to start with is inclusive and the number you want to end with is exclusive. So with the first loop, a, it starts by assigning the value 0 to the variable a for that particular cycle of the loop, and then goes on to perform all the other instructions inside that loop. It continues to cycle through the loop until the value of the variable reaches 6. Once the value reaches 6, it does not go through the loop again. So the loop gets cycled through six times: 0, 1, 2, 3, 4 and 5. But the possible digits in the combination are 1 through 6, not 0 through 5, so when we are defining the string combo, we say it is str(a+1), etc., instead of str(a) to get a meaningful answer.
So why not just make the loop go from 1 to 7? We could do that, and it would work perfectly fine. But there is a reason I start with 0. Everything in lists and strings and other things in Python start with the value 0. For example, the string combo has four characters in it. Python refers to the position of each character as 0, 1, 2 and 3, not 1, 2, 3 and 4. So I usually make my loops start with 0 instead of 1, just so I keep myself in the mindset of everything beginning with 0 in Python. If I ever write anything more complicated with lots of variables and lists and strings and references to positions within strings and lists, it will help prevent confusion because I stay consistent with starting everything at 0.
Now, I have a list of 302 combinations I need to try to get my bike unlocked. I need to get busy.
Related posts:
- The bike saga continues
- Practical programming
- Programming with Python
- Python, in bitesize chunks
- timestable.py



