This code will draw a grid with 2 rows and 2 column, then we can simplify the program by making it shorter. And we will draw another grid with 4×4 grid.
Code for Drawing a Grid:
def do_twice(f):
“””This function invokes twice the function passed as
parameter”””
f()
f()
def do_four(f):
“””This function invokes four times the function passed as
parameter. It makes use of the do_twice function.”””
do_twice(f)
do_twice(f)
def print_grid():
“””This function prints the grid using do_twice”””
do_twice(print_row)
print_beams()
def print_beams():
do_twice(print_beam)
print(‘+’)
def print_posts():
do_twice(print_post)
print(‘|’)
def print_row():
print_beams()
do_four(print_posts)
def print_beam():
“””This function prints a beam”””
print (‘+ – – – – ‘, end=”)
def print_post():
“””This function prints a post”””
print (“| “, end=””)
def print_beams():
“””This function prints a beams of a grid”””
print_beam()
print_beam()
print (‘+’)
def print_posts():
“””This function prints a posts of a grid”””
print_post()
print_post()
print(‘|’)
def print_row():
“””This function print a row of the grid”””
print_beams()
print_posts()
print_posts()
print_posts()
print_posts()
def print_grid():
“””This function print a grid”””
print_row()
print_row()
print_beams()
def print_newbeams():
do_four(print_beam)
print(‘+’)
def print_newposts():
do_four(print_post)
print(‘|’)
def print_newrow():
print_newbeams()
do_four(print_newposts)
def print_newgrid():
“””This function prints the grid using do_four”””
do_four(print_newrow)
print_newbeams()