LAB 4

Writing a program that draws a grid like the following:

1. Creating the functions: print_beam()  and  print_post()

Source Code:

def print_beam():
“””This function prints a beam”””
print (‘+ – – – -‘),

def print_post():
“””This function prints a post”””
print (‘| ‘),

Output:

2. Printing the beams and the posts

Source Code:

def print_beam():
“””This function prints a beam”””
print (‘+ – – – -‘),

def print_post():
“””This function prints a post”””
print (‘| ‘),

def print_beams():
“””This function print the beams of a grid”””
print_beam()
print_beam()
print (‘+’)

def print_posts():
“””This function print the posts of a grid”””
print_post()
print_post()
print (‘|’)

Output:

3. Printing a row

Source Code:

def print_beam():
“””This function prints a beam”””
print (‘+ – – – -‘),

def print_post():
“””This function prints a post”””
print (‘| ‘),

def print_beams():
“””This function print the beams of a grid”””
print_beam()
print_beam()
print (‘+’)

def print_posts():
“””This function print the 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()

Output:

4. Printing the grid

Source Code:

def print_beam():
“””This function prints a beam”””
print (‘+ – – – -‘),

def print_post():
“””This function prints a post”””
print (‘| ‘),

def print_beams():
“””This function print the beams of a grid”””
print_beam()
print_beam()
print (‘+’)

def print_posts():
“””This function print the 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 calls print_row() twice and then print_beams()”””
print_row()
print_row()
print_beams()

Output:

5. Improving the program by using do_twice(f)  and  do_four(f)

Source Code:

def do_twice(f):
“””This function invokes twice the function passed as a parameter”””
f()
f()

def do_four(f):
“””This function invokes four times the function passed as a parameter.
It makes use of the do_twice function.”””
do_twice(f)
do_twice(f)

def print_beam():
“””This function prints a beam”””
print (‘+ – – – -‘),

def print_post():
“””This function prints a post”””
print (‘| ‘),

def print_beams():
“””This function prints the beams of a grid by using the do_twice function”””
do_twice(print_beam)
print (‘+’)

def print_posts():
“””This function prints the posts of a grid by using the do_twice function”””
do_twice(print_post)
print (‘|’)

def print_row():
“””This function prints a row of the grid by using the do_four function”””
do_four(print_posts)
print_beams()

def print_grid():
“””This function prints the grid using do_twice”””
print_beams()
do_twice(print_row)

Output:

6. Same program now showing a grid with four rows and four columns

Source Code:

def do_twice(f):
“””This function invokes twice the function passed as a parameter”””
f()
f()

def do_four(f):
“””This function invokes four times the function passed as a parameter.
It makes use of the do_twice function.”””
do_twice(f)
do_twice(f)

def print_beam():
“””This function prints a beam”””
print (‘+ – – – -‘),

def print_post():
“””This function prints a post”””
print (‘| ‘),

def print_beams():
“””This function prints the beams of a grid by using the do_four function”””
do_four(print_beam)
print (‘+’)

def print_posts():
“””This function prints the posts of a grid by using the do_four function”””
do_four(print_post)
print (‘|’)

def print_row():
“””This function prints a row of the grid by using the do_four function”””
do_four(print_posts)
print_beams()

def print_grid():
“””This function prints the grid using do_four”””
print_beams()
do_four(print_row)

Output: