BYU logo Computer Science

To start this assignment, download this zip file.

Lab 6b β€” Grids

Reminder, you should work in teams of 2 or 3 when solving the lab problems. Learning to code together is an important part of this class.

Preparation

1 minute

Download the zip file for this lab, located above. This zip file has code that you will use for this assignment. Extract the files and put them in your cs110 directory in a folder called lab6b.

Grids practice

10 minutes

We have given you code in practice.py that provides some simple practice with grids.

There are instructions in the code asking you to do the following:

  1. Copy and paste a function for creating an empty grid

  2. Copy and paste a function for printing a grid

  3. Create an empty grid, 5 rows and 8 column; use a space as the value for the grid

  4. Put a β€™πŸ¦‹β€™ in (1, 1) β€” row 1 β€” column 1

  5. Put a β€™πŸ¦–β€™ in (0, 7)

  6. Put a β€™πŸ³β€™ in (4, 2)

  7. Print the grid

You can see the guide on grids for help.

After you finish and run the program, it should print:

              πŸ¦–
  πŸ¦‹


    🐳

Discuss with the TA

  • How did you write this code? Show a solution and discuss.
  • Is there anything you don’t understand about grids?

Drawing

20 minutes

We have a partially-written program in drawing.py that will use a grid to create a simple drawing program. Following are the functions for you to complete.

Printing the grid

First write the print_grid() function:

def print_grid(grid: list[list[str]], between: str = ' '):
    """
    Print all the items in the grid, so that it looks like a grid.
    <between> is the character between columns, by default a space
    """
    # Write code here
    pass

Notice that there is an important difference between this function and the one included in the guide on grids. This function takes a new keyword parameter, between. This character should be used as the character to print in between columns.

You can copy and paste the function from the guide and then modify it to have this new functionality, which will let you specify the character to use in between columns when printing a grid.

Drawing in one row and column

Next write the draw() function:

def draw(grid: list[list[str]], row: int, column: int, character: str):
    """
    <grid> - a grid
    <row> - a row number (integer)
    <column> - a column number (integer)
    <character> - a character

    Modifies the grid so that <row>,<column> contains <character>.
    Does not modify the grid if <row> is too small or too large.
    Also does not modify the grid if <column> is too small or too large.
    """
    # Write code here
    pass

This is similar to the practice problem, except you need to be sure the row and column you are given are actually in the grid. We did something like this for the has_value() function in the guide.

Drawing on the grid

Finally, write the draw_on_grid() function:

def draw_on_grid(grid: list[list[str]]):
    """
    This function allows a user to draw on a grid. It does the following:
    - prints the grid, using '' as the character between columns
    - lets the user enter coordinates in "row, column" format, e.g.:
        2, 3
    - draws a 🟦 at each coordinate they enter
    - stops when they enter nothing
    """
    # Write code here
    pass

This function needs to loop, take user input, and then modify the grid.

Discuss with the TA

  • How did you implement these functions? Show a solution and discuss.
  • Make sure everyone understands every line of code we supplied.

Grading

To finish this lab and receive a grade, take the canvas quiz.

Solution

We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)