I am new and am making Al Sweigar book at the moment. In an exercise in chapter 4, he asks the following:
Let's say you have a list of lists, where each value in the internal lists is a single-character string, for example:
grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']]
You can think of the grid [x] [y] as a character in the x- and y-coordinates of a picture with text characters. The origin (0, 0) will be in the upper left corner, the coordinates of the x-coordinates go to the right, and the w y-coordinates increase. Copy the previous grid value and write the code that uses it to print the image.
..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O....
So, I wrote the code, and it does what it asks for, but I think it is very poorly written, and I wanted to ask you how I can improve it. My code
grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] newString = '' for i in range(len(grid)): newString += str(grid[i][0]) newString1 = '\n' for i in range(len(grid)): newString1 += str(grid[i][1]) newString2 = '\n' for i in range(len(grid)): newString2 += str(grid[i][2]) newString3 = '\n' for i in range(len(grid)): newString3 += str(grid[i][3]) newString4 = '\n' for i in range(len(grid)): newString4 += str(grid[i][4]) newString5 = '\n' for i in range(len(grid)): newString5 += str(grid[i][5]) print(newString+newString1+newString2+newString3+newString4+newString5)
Program Output:
..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O....