First, guys. Please post your code in brackets. There's a link that reads "BBCode" right above the textbox if you don't know how.
Second, ibraheemmohsen, please make basic stuff first. It looks like you're skipping over a lot of crucial programming concepts. You'll really need classes, for instance. Also debugging skills. Also the ability to explain what errors you're getting instead of saying that something went wrong. We're not mind readers, you know.
@ambro86, I disagree. We don't need a BGT-like guide for Python because it's just going to lock people into the mindset that we have been pushing them out of, it being the fact that you don't need an audiogame-specific tutorial. That's partially the problem: When people get stuck they try to look for blind-specific concepts where there are none. The other problem is that people rarely actually read through a programming book in full. Your question about 2D arrays, for instance, shows that you haven't put together what is meant by indexing a list. Let's suppose we have a 5x5 list filled with zeros
matrix = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
When you loop over it, the outer loop controls access to the first layer, or set of lists: consider this code:
matrix = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
for i in range(len(matrix)):
print(matrix[i]) # Outputs [0, 0, 0, 0, 0] every single time you iterate, regardless of index
If you picture a matrix as a box and think about what is happening, you'll see that your outer index is moving through the rows rather than the columns. This is why a lot of loops actually read as
for y in range(...):
for x in range(...):
# ...
Going back to the box analogy, if you stick a print instead of the comment above, like so,
matrix = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
for y in range(5):
print("New row")
for x in range(5):
print(matrix[y][x])
You'll get an output that looks like this (I only paste the first two rows because others are exactly the same)
New row
0
0
0
0
0
New row
0
0
0
0
0
So your outer index moves up and down through the first set of lists, while your inner index moves from left to right. It's still within exactly the same row every single time, hence a box. (Please forgive me for using "up" and "down", I'm fully aware that's not what is happening under the hood but that's my best shot at providing an explanation)
Quick recap. Your outer loop moves through rows, and your inner loop moves through columns. Your question of how to change a single column, then, should be relatively straight forward. Let's suppose we have the same 5x5 matrix and we want to change the middle column to ones
matrix = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
for row in range(len(matrix)):
matrix[row][2] = 1
print(matrix) # [[0, 0, 1, 0, 0]...]
Also, before you ask. No. It doesn't have to be a box. It could be a rectangle, like if I gave you a 5x3 matrix instead. The concept is still the same--you're accessing a list of lists. From here it's rather intuitive to derive the formula for accessing an element within a flattened array, but that is left as an exercise to the reader