CSc 250: Lab 4

This lab was written and designed to be completed during lab-time for cs250. This lab is not graded and does not need to be turned in. However, you should work through all of the problems, as it will help you on assignments and exams. If you cannot complete the lab in the alloted time, feel free to continue at home.

Precursor

Throughout the problems in this lab, we will be writing several python functions in the same file. Create a new python script file named shapes.py and open it up in your editor of choice (atom). On the first line of this file, write a descriptive comment, for example:

# A file with functions to print out various shapes

Put all of the functions from all problems in this file as you go along.

Problem 1 - line

Write a function named line in shapes.py that prints a line of - (dash) characters. This function will take one argument named length, which will specify the length of the line. Also make sure to put a descriptive comment immediately above the function, documenting what it does, what it returns, and the role of each argument. For example:

# ####### Print a line to stdout #######              
# line is the length of the line to be printed
# This function does not return a value 

Once you’ve written the function, we will want to test it out. A convenient way to do this is to try the function out in the interactive python shell. We could copy-paste the function into the shell directly, and then use it, but that isn’t very idiomatic! Instead, we will import the function into the interactive shell.

Open up a new terminal window, and cd into the directory that contains shapes.py. Once in the right directory, start a python 3 shell (run python3.X). You should put your editor with shapes.py and this new terminal window side-by-side. Your screen should look like:

Now, we will import the functions from shapes.py as a module into the shell session. To do so, go ahead and run:

>>> import shapes

Running this will search for a file named shapes.py in the current working directory. If it finds one (it should, in this case), it will import all of the functions from it as a new module in the session. By default, the name of the module is the same as the name of the file (with the .py part dropped). To check if the module imported correctly, type the name of the module and hit enter. You should see something like this:

>>> shapes
<module 'shapes' from '<redacted>/cs250/labs/lab-4/solutions/shapes.py'>

So far, we have only written one function in shapes.py. We can check which function(s) are in this module (or any module) by passing the name of the module to the built-in help() function. If we run:

>>> help(shapes)

You should be presented with text that looks roughly like this:

Help on module shapes:

NAME
    shapes - # A file with functions to print out various shapes

FUNCTIONS
    line(length)
        # ####### Print a line to stdout #######
        # line is the length of the line to be printed

FILE
<redacted>/cs250/labs/lab-4/solutions/shapes.py

Only one function is listed in the FUNCTIONS section, which is expected. Also, notice how the help() info incorporates the comments that we wrote. By doing so, we can customize what is seem when we (or another use) tried to run help() on a module that we write.

Finally, let’s try running line(). To run it, we cannot just type:

>>> line(5)

This is because the function was imported as a component of the shapes module, not all by itself. Instead, we must run:

>>> shapes.line(5)

This is telling the python shell “run the function line which exists in the module named shapes, and pass it the argument 5”.

Below are a few sample calls to this function. The result of running your function should look similar.

>>> shapes.line(3)
---
>>> shapes.line(7)
-------
>>> shapes.line(20)
--------------------
>>> shapes.line(70)
----------------------------------------------------------------------

Problem 2 - Square

Next, write another function in shapes.py named square that prints a square of X characters. This function will take one argument named width, which will specify the width (and height) of the square. Write the function, put an informative comment above it, and then re-save shapes.py.

You should test this function in the same python interactive shell you used for problem 1. However, if you try to run:

>>> shapes.square(3)

This does not work! The problem is that when we import-ed shapes the first time, it was before the square function had been written. If a module is imported from a file, and then the file is modified, the modifications are not automatically updated in the module withing the python session. This can be seen if we run help(shapes) again. You’ll see something like:

Help on module shapes:

NAME
    shapes - # A file with functions to print out various shapes

FUNCTIONS
    line(length)
        # ####### Print a line to stdout #######
        # line is the length of the line to be printed

FILE
<redacted>/cs250/labs/lab-4/solutions/shapes.py

Which does not display anything about the square function. In order to get the updated contents of the module, we must reload the module. How does one reload a module?

In order to reload shapes, we first need to import another module called importlib. importlib is a module built in to python by default. Thus, when it is imported, we don’t need to have the importlib.py file in the current working directory. This module has a function called reload which can be used to reload modules that are already imported. Thus, to reload shapes, you will need to run:

>>> import importlib
>>> importlib.reload(shapes)
<module 'shapes' from '<redacted>/cs250/labs/lab-4/solutions/shapes.py'>

After doing this, running help(shapes) will show something like:

Help on module shapes:

NAME
    shapes - # A file with functions to print out various shapes

FUNCTIONS
    line(length)
        # ####### Print a line to stdout #######
        # line is the length of the line to be printed

    square(width)
        # ####### Print a square to stdout #######
        # width is the width/height of the square

FILE
<redacted>/cs250/labs/lab-4/solutions/shapes.py

Now there is an entry for the square function. As with line, square can be called by running shapes.square(5). Below are a few sample calls:

>>> shapes.square(3)
XXX
XXX
XXX
>>> shapes.square(7)
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX

Problem 3 - Rectangle

Write another function in shapes.py named rectangle that prints a rectangle of characters. This function will take three arguments. The first is the width of the rectangle, The second is the height of the rectangle. The third is a character, which the rectangle will be made up of. Write the function, add a descriptive comment immediately above it, and re-save shapes.py.

In order to use this function, follow the same steps for re-loading the shapes module described in problem 2. Also, make sure to check that the function description is added to the help(shapes) section.

Below are a few sample calls:

>>> shapes.rectangle(5, 3, '+')
+++++
+++++
+++++
>>> shapes.rectangle(38, 5, '#')
######################################
######################################
######################################
######################################
######################################
>>> shapes.rectangle(4, 15, 'O')
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO
OOOO

Problem 4 - Checker Board

Write another function in shapes.py named checker_board. This function will print a checker board with the dimensions given by the user. This function will take four arguments:

The first is the width/height of each tile on the board. This number should be a multiple of two! The second is the number of tiles along each edge. This number should also be a multiple of two! The third is the character to use for the “dark” tiles of the board. The fourth is the character to use for the “light” tiles of the board.

Write the function, add a descriptive comment immediately above it, and re-save shapes.py.

In order to use this function, follow the same steps for re-loading the shapes module described in problem 2. Also, make sure to check that the function description is added to the help(shapes) section.

Below are a few sample calls:

>>> shapes.checker_board(2, 4, '#', '-' )
##--##--
##--##--
--##--##
--##--##
##--##--
##--##--
--##--##
--##--##
>>> shapes.checker_board(1, 16, 'X', ' ' )
X X X X X X X X 
 X X X X X X X X
X X X X X X X X 
 X X X X X X X X
X X X X X X X X 
 X X X X X X X X
X X X X X X X X 
 X X X X X X X X
X X X X X X X X 
 X X X X X X X X
X X X X X X X X 
 X X X X X X X X
X X X X X X X X 
 X X X X X X X X
X X X X X X X X 
 X X X X X X X X
>>> shapes.checker_board(4, 4, '#', ' ' )
####    ####    
####    ####    
####    ####    
####    ####    
    ####    ####
    ####    ####
    ####    ####
    ####    ####
####    ####    
####    ####    
####    ####    
####    ####    
    ####    ####
    ####    ####
    ####    ####
    ####    ####
>>> shapes.checker_board(4, 4, '-', '+' )
----++++----++++
----++++----++++
----++++----++++
----++++----++++
++++----++++----
++++----++++----
++++----++++----
++++----++++----
----++++----++++
----++++----++++
----++++----++++
----++++----++++
++++----++++----
++++----++++----
++++----++++----
++++----++++----

Problem 5 - Custom shapes

Add three additional functions to shapes.py that print some shape or text art to standard output. Each one of these function should take at least one argument that somehow manipulates the text that is printed. For example, you might write a function called stick_figure that takes a single numeric argument, which adjusts the height of the figure that is printed. The details of what you want these functions to do, what arguments they will take, and what exactly they print is up to you, so be creative! Make sure that you give each function a descriptive name, give each of the arguments a descriptive name, and add a descriptive comment above each one.

Once you’ve written them:

Summary

By the end of this lab, shapes.py should have 7 functions in it. You have now created your very own (simple) module to use in the python shell, or from other python programs! We will talk more about modules in future lectures, but this lab acts as a nice warm-up to functions and modules.

Click here for the solution to problems 1-4