Issue
I want to make a 8×8 chessboard with 3×3 for each tile in Python. But there are problems with the checkering. I can't really show what it prints out, so you will have to run it yourself. Here is my code:
print("""
Chess
Press Enter to Start...
""")
enterwait= input()
if enterwait= "":
line = []
chessboard = ""
changeColorAllowed = False
lineQueue = False
queue = 0
for linecount in range(24):
for i in range(24):
if i%3==0:
changeColorAllowed = not changeColorAllowed
if changeColorAllowed == False:
if linecount%3==0 or linecount%4==0 or linecount%5==0:
line.append([queue,"white", "a" + str(i//3), "primary", "none"])
else:
line.append([queue,"black", "a" + str(i//3), "primary", "none"])
else:
if linecount%3==0 or linecount%4==0 or linecount%5==0:
line.append([queue,"black", "a" + str(i//3), "primary", "none"])
else:
line.append([queue,"white", "a" + str(i//3), "primary", "none"])
for i in line:
if len(chessboard)%25==0:
chessboard+="\n"
if i[1] == "white":
chessboard += "⬜"
else:
chessboard += "⬛"
print(chessboard)
Any help is appreciated!
bad checkering on my python chessboard
Solution
The condition if linecount%3==0 or linecount%4==0 or linecount%5==0
makes little sense. There is nothing in this grid that repeats every 4 or 5 lines.
The body of the first nested loops, that populates line
, can be fixed with this body:
if (linecount // 3) % 2 == (i // 3) % 2:
line.append([queue,"white", "a" + str(i//3), "primary", "none"])
else:
line.append([queue,"black", "a" + str(i//3), "primary", "none"])
But you can do the whole chessboard with much less code, making use of the *
operator on strings and lists:
line = "⬜⬜⬜⬛⬛⬛" * 4
chessboard = "\n".join(([line] * 3 + [line[::-1]] * 3) * 4)
print(chessboard)
Answered By - trincot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.