I'm newbie and doing Al Sweigar's book at the moment. In chapter 4's exercise, he asks the following,
Say you have a list of lists where each value in the inner lists is a one-character string, like this:
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 grid[x][y] as being the character at the x- and y-coordinates of a “picture” drawn with text characters. The (0, 0) origin will be in the upper-left corner, the x-coordinates increase going right, and w the y-coordinates increase going down. Copy the previous grid value, and write code that uses it to print the image.
..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
...OOO...
....O....
So I have written the code and it does what he asks for but I think its very poorly written and I wanted to ask you how can I 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)
Output of program:
..OO.OO..
.OOOOOOO.
.OOOOOOO.
..OOOOO..
...OOO...
....O....
Practical Programming for Total Beginners In Automate the Boring Stuff with Python, you'll learn how to use Python to write programs that do in minutes what would take you hours to do by hand - no prior programming experience required.
It's a must buy for beginners. This book can take you from having no programming experience whatsoever to being able to read and write Python. The fruits of your hard work: the able to scrape the web, automate things, and make your life easier!
AUTOMATE THE BORING STUFF WITH PYTHON, 2ND EDITION. For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc.
The expression causes an error because 99 is an integer, and only strings can be concatenated to other strings with the + operator. The correct way is I have eaten ' + str(99) + ' burritos.
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', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
for i in range(len(grid[0])):
print()
for j in range(len(grid)):
print(grid[j][i], end='')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With