from ipythonblocks import BlockGrid
grid = BlockGrid(10, 10, fill=(123, 234, 123))
grid
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
Modify the next cell to contain two for loops. The first for loop should set the last row to red. The second for loop should set the last column to white.
grid = BlockGrid(10, 10, fill=(123, 234, 123))
for i in range(10):
grid[9, i] = RED
for i in range(10):
grid[i, 9] = WHITE
grid
# Note -- you could do this in a single loop pretty easily:
grid = BlockGrid(10, 10, fill=(123, 234, 123))
for i in range(10):
grid[9, i] = RED
grid[i, 9] = WHITE
grid
Write a for loop so that every other cell along the diagonal is white. Your final grid should look like the following, BUT with using a for loop instead.
Hint: you want to color on an even set of numbers. You can either multiple i by 2 (i*
2) or write an 'if' statement to test if i % 2 == 0.
grid = BlockGrid(10, 10, fill=(123, 234, 123))
grid[0, 0] = WHITE
grid[2, 2] = WHITE
grid[4, 4] = WHITE
grid[6, 6] = WHITE
grid[8, 8] = WHITE
grid
# one way to do this is to use an if statement:
grid = BlockGrid(10, 10, fill=(123, 234, 123))
for i in range(10):
if i % 2 == 0: # if even
grid[i, i] = WHITE
grid
# you can also do this by multiplying by 2:
grid = BlockGrid(10, 10, fill=(123, 234, 123))
for i in range(5): # <-- only go up to 5
j = i*2
grid[j, j] = WHITE
grid
# or, if you go read up on the range function, you can have the range function do the counting for you!
grid = BlockGrid(10, 10, fill=(123, 234, 123))
for i in range(0, 10, 2): # <-- step by 2
grid[i, i] = WHITE
grid
Write a nested for loop so that you color each row progressively more red, starting at black. (See definition of RED above.) Your final grid should look like the below, but with rows getting redder instead of columns.
grid = BlockGrid(10, 10, fill=(123, 234, 123))
grid[:,0] = (0, 0, 0)
grid[:,1] = (10, 0, 0)
grid[:,2] = (20, 0, 0)
grid[:,3] = (30, 0, 0)
grid[:,4] = (40, 0, 0)
grid[:,5] = (50, 0, 0)
grid[:,6] = (60, 0, 0)
grid[:,7] = (70, 0, 0)
grid[:,8] = (80, 0, 0)
grid[:,9] = (90, 0, 0)
grid
# probably the simplest way to do it is like so:
grid = BlockGrid(10, 10, fill=(123, 234, 123))
for i in range(10):
grid[i, :] = (i*10, 0, 0)
grid
# but, there are many, many ways to skin a cat:
grid = BlockGrid(10, 10, fill=(123, 234, 123))
for i in range(0, 100, 10):
grid[i / 10, :] = (i, 0, 0)
grid