Uber-newb question. I thought I was over this, but I am stuck.
First I have I'll tell you what I am trying to achieve. Newbie Challenge question. (credit: V. Anton Spraul)
You'll see from the picture that I am trying to create a 3 digit unique code (corresponding to Fire, Police, Sanitation), that adds to 12 and that the middle digit (Police) is always even.
I know there is a brute force way of doing this but I didn't want to take that approach. I thought I'd take the slightly more difficult but hopefully efficient way by stacking 3 for loops. But I am stuck at a very early stage and can't understand the logic of what's going on. Here's my code:
highestdigit=7
department_number=[0,0,0]
department_numbers=[]
for number in range(1,highestdigit+1):
#Fire, Police, Sanitation
#Numbering the Police dept
if number %2 == 0:
department_number[1]=number
department_numbers.append(department_number)
else:
pass
print(department_numbers)
What I am expecting from this is:
[[0,2,0,],[0,4,0],[0,6,0]]
What I getting is:
[[0,6,0,],[0,6,0],[0,6,0]]
Why is this? Why am I getting the max value digit in the range, and why 3 times?
Also if I try to place the print command within the loop, it
highestdigit=7
department_number=[0,0,0]
department_numbers=[]
for number in range(1,highestdigit+1):
#Fire, Police, Sanitation
#Numbering the Police dept
if number %2 == 0:
department_number[1]=number
department_numbers.append(department_number)
print(department_numbers)
else:
pass
What I was expecting from this:
[[0,2,0]]
[[0,2,0],[0,4,0]]
[[0,2,0],[0,4,0],[0,6,0]]
i.e. that it would build the list of lists that I want, step by step, printing each step.
Instead, what I got is:
[[0, 2, 0]]
[[0, 4, 0], [0, 4, 0]]
[[0, 6, 0], [0, 6, 0], [0, 6, 0]]
I am pretty baffled why either of these outcomes are occurring.