So I know this will likely be a simple answer to this question, but I'm learning some basic python and just did a lesson where you learn to create what looks like tic-tac-toe fields by defining a function and using for loops.
Here is the code:
def drawField(): #we define a funtion using def.
for row in range (5): #01234
if row%2 == 0: # if the row is even
for column in range (5): #Then for rows in the range 5(0,1,2,3,4)
if column%2 ==0: #(if the column is even)
if column!=4: # and not equal to 4
print(" ", end ="") #print a space and add the next string. Do not add a new line.
else:
print(" ") #or else for rows that are equal to 4, just print a space and end by starting a new line.
else:
print("|", end = "") #For rows that are uneven print | and don't start a new line but continue in the same line.
else:
print("-----") #For all uneven rows just print --------
drawField() #call the drawfield function.The comments are me going through the code to make sure I know exactly what is happening.
I know it's probably dumb, but at first, I really thought row and column might be keywords that the language recognises, despite having learned about variables. Anyway, just to confirm, I changed the variables to simple letters and I still got my result.
What I'm thinking as I'm writing this is that initially, it's just two normal numeric series and then the end="" parameter is what actually determines the layout. Yeah, that sounds right to me actually but I would appreciate any confirmation because I could be totally off haha.
Thanks in advance!
» pip install rows
You just need to change your loop so you go to new line after every row. You can also use for in range like this
for row in range(nRow):
for col in range(nCol):
print(ch, end=' ')
print()
Put rows in the outer loop and columns in the inner loop. At the end of each row reset the column counter and print a newline.
while j<=nRow:
while i<=nCol:
print(ch, end='')
i=i+1
i = 1
print()
j=j+1