Your code has a few issues that are preventing it from generating a proper chessboard pattern. A chessboard pattern for a given size (e.g., 3) should produce an n x n grid where the values alternate between 1 and 0, like this:
code
1 0 1
0 1 0
1 0 1
However, your code outputs something like this for chessboard(3):
code
101
101
101
Let me break down the problems in your code step by step, explain what's wrong, and suggest how to fix it.
1. Logical Issue in the Loop Structure
Your code uses a single while loop to handle both rows and columns, but the logic inside it doesn't properly alternate between 1 and 0 for each position in the grid.
Specifically:
The loop always starts by printing "1" for each iteration.
It then checks if counter == size. If it is, it moves to the next row (by decrementing rows and printing a newline) and skips the rest of the loop using continue.
If counter != size, it prints "0".
This results in each row being printed as "101" (for size=3), because the pattern is forced to alternate in a fixed way ("1" then "0") without considering the actual position in the grid.
As a result, the output doesn't alternate correctly across rows and columns. For example:
The first row should start with "1", but the second row should start with "0" to create the chessboard effect.
Why this happens:
The continue statement inside the first if block skips the printing of "0" when counter == size after printing "1". This means the loop doesn't handle the alternation properly for the end of each row.
The code doesn't account for the row index when deciding whether to print "1" or "0". In a chessboard, the decision should depend on the sum of the row and column indices (e.g., if (row + column) % 2 == 0, print "1"; otherwise, print "0").
2. The continue Statement Causes Skipping
When counter == size after printing "1", the continue statement jumps back to the start of the while loop, skipping the second print("0", end="") and its associated logic.
This leads to incomplete rows in some cases, but in your example, it just results in repetitive patterns.
3. No Proper Nested Structure
A chessboard requires handling rows and columns separately, which typically means using nested loops (one for rows and one for columns).
Your code tries to combine everything into a single loop, which makes it hard to track the row and column positions.
4. Typo in the Main Guard
At the end of your code, you have:
code
if name == "main":
chessboard(3)
This should be:
code
if __name__ == "__main__":
chessboard(3)
The variable is actually __name__ (with double underscores), not name. This is a common typo and will cause the code to not run as expected if you're trying to execute it as a script.
Suggested Fix
To fix this, rewrite the function to use nested loops. This is the standard way to generate a grid like a chessboard. Here's a corrected version:
python
def chessboard(size):
for i in range(size): # Loop for each row
for j in range(size): # Loop for each column
if (i + j) % 2 == 0: # Check if the position should be 1 or 0
print("1", end=" ") # Print 1 if (i + j) is even
else:
print("0", end=" ") # Print 0 if (i + j) is odd
print() # Move to the next line after each row
# Main guard
if __name__ == "__main__":
chessboard(3)
How this works:
The outer loop (for i in range(size)) handles the rows.
The inner loop (for j in range(size)) handles the columns.
For each cell at position (i, j), we check if (i + j) % 2 == 0. If true, print "1"; otherwise, print "0".
After each row, we print a newline to move to the next row.
Output for chessboard(3):
code
1 0 1
0 1 0
1 0 1
This will produce the correct alternating pattern. You can adjust the end=" " in the print statements if you want to change the spacing (e.g., remove the space for no gaps).
Why This Approach is Better
It's simpler and more readable.
It directly maps to the 2D structure of a chessboard.
It avoids the counter and continue issues in your original code.
If you have any more questions or want to modify this further (e.g., for different patterns or sizes), let me know!