I am currently working on a project (In Python 2.7), and ran into a problem when the for
values for range()
was printed twice.
I minimized a lot of the part I am talking about, so if just assume that there are other parts in my code.
So, basically, I have a 5 by 5 board, and depending on case1
and case2
, I am copying the board with list()
and making alters to it. Then, I want to have a column of numbers stating the row numbers, so that the output will be something like:
1 O O O O O
2 O O O O O
3 O O O O O
4 O O O O O
5 O O O O O
for case1
and case2
.
I have:
board = []
# Creating board
for i in range(5):
board.append(["O"] * 5)
case1 = True
case2 = True
if case1 == True:
board1 = list(board) #Making duplicate of board
# I took out a lot of the code making alterations to board1
for i in range(5):
board1[i].insert(0, str(i + 1) + " ")
if case2 == True:
board2 = list(board) #Making duplicate of board
# I took out a lot of the code making alterations to board2
for i in range(5):
board2[i].insert(0, str(i + 1) + " ")
#Printing the boards
for i in range(5):
print " ".join(board1[i])
print "\n\n" #Just a gap
for i in range(5):
print " ".join(board2[i])
However, the output is:
1 1 O O O O O
2 2 O O O O O
3 3 O O O O O
4 4 O O O O O
5 5 O O O O O
for each board1
and board2
. I get the expected result when I take out everything concerning case2
, however, with my current code, it is printing the range() for the rows printing twice.
I would like to know if there are ways to solve this problem.
Sorry if my wording is very unclear. I would be thankful if somebody to edited the question/title for more clarity.