Thanks to Dr. Alan McLeod for the content of this lab.

Exercise 1 - Error Checking

Alter your list generating function from last week's lab so that it checks for legal values for the number of rows and the number of columns. Let's say that they both must lie between 3 and 20, inclusive. Raise a ValueError exception with an appropriate message if they are not legal.

Be prepared to catch this exception in main(). You have learned how to do both of these things in lecture! Test your program with legal and illegal values to make sure the exception mechanism works. Here is what you might have ended up with.

Exercise 2 - Default Arguments and Keyword Parameters

Further alter your generating function to use default sizes of 10 for both the number of rows and the number of columns. Alter your main() function to first generate a list of the default size by not supplying any parameters and then use a single keyword parameter to change the number of columns to 15. Do you really need the try/except construct when using the generating function with no arguments?

Here is what you might have ended up with.

Exercise 3 - Working With Strings

The last lecture contained detailed descriptions of some of the many string methods in Python. Instructions on how to view the Python documentation for these methods was described in Exercise 3 of the lab for Week 6. You should try working with most of these methods on the command line. Improvise creating values on which to call the methods; use values and arguments that will produce a successful result and ones that won't; try overriding the defaults. Familiarize yourself with what is available.

As an exercise, write a function with one argument: a list or tuple of integers. Return a single string that contains all of the integers in the collection separated by commas. Write another function which takes such a string as a parameter and returns the corresponding list or tuple. For example, [2, 18, 7, 99, 54, 3] will have a corresponding string "2, 18, 7, 99, 54, 3" which will translate back into [2, 18, 7, 99, 54, 3]. There is one string method in particular which would prove useful for the second function.

Exercise 4 - "Drawing" Program

You can now combine some of the exercises from this week and last week into a larger, multi-function program.

Consider creating a "canvas" that consists of a list of lists, where the individual elements of the list consist of a single character, typically a space. You "draw" to this canvas by changing individual elements of the list of lists. Consider drawing points (an individual character position), squares, rectangles, circles as well as adding strings to your drawing. Your coordinate system should consider the top, left corner of the canvas as (0, 0). Circles, squares, points, rectangles and strings need to know their top and left positions. Circles, squares and rectangles also need to know their width and height. Increasing top moves the drawn object lower on the canvas. They also need to know their border character and their fill character. Pass the canvas to drawing functions by reference. Use as many default arguments as you can.

You will need to raise a ValueError exception if parameters are not legal. You could encounter an illegal canvas size or an attempt to draw something outside the borders of the canvas.

Try to figure out what functions you need first. If you cannot, here is a possible list of functions that you could write for this exercise. Next, figure out what each function needs to know to do its job. Which parameters can be written with default arguments? What should each function return? Here is a more detailed list of functions with parameter lists and return types.

Finally, here is a complete program sans comments.