MONT 105S, Spring 2009

Home | | Course Schedule | | Assignments | | Lecture Notes

Laboratory 6
Due at the end of today's class

Problem 1:
Write a program that has two functions: drawX( ) and drawO( ). The functions should have instructions for drawing and "X" or drawing an "O" in the lower left part of the screen. The main part of the program should ask the user whether he or she wants to draw an X or an O. It should call the appropriate function to draw whichever the user chooses.

The following code can draw an X:

    yertle.up( )
    yertle.goto(-70, -30)
    yertle.down( )
    yertle.right(45)
    yertle.forward(57)
    yertle.up( )
    yertle.left(45)
    yertle.backward(40)
    yertle.left(45)
    yertle.down( )
    yertle.forward(57)
    yertle.right(45)

The following code can draw an O:

    yertle.up( )
    yertle.goto(-50, -70)
    yertle.down( )
    yertle.circle(20)

You should use the following template for your program:

The following is a sample output of the program:
Sample output 1:

Do you want to draw and X or an O? X


Problem 2: Once you have saved and printed out your program for problem 1, save it under a new name, e.g. drawXorOposition.py. Modify the two functions so that they now each take two parameters, a row number and a column number. The functions should now draw the X or the O at the position given by the row and column numbers (imagine 3 rows and 3 columns as in a tic-tac-toe game). You can have the turtle draw the X or O in a different place by changing the goto( ) command in each function.

For the drawX(row,col) function, change the goto( ) line to read:

For the drawO(row, col) function, change the goto( ) line to read:

Thus, as you increase the row number by 1, the position of the drawn X or O will shift up by 50 pixels. As you increase the column number by 1, the position of the drawn X or O will shift right by 50 pixels.

Now modify your main program to ask the user whether they want to draw an X or an O and what row and column they would like to draw it in. The program should then call the appropriate function, based on the user's answer, to draw the X or O in the appropriate place.

You may use the following program template:

The following are sample outputs of the program:
Sample output 1:

Do you want to draw and X or an O? O
Enter the row number: 0
Enter the column number: 2

Sample output 2:

Do you want to draw and X or an O? X
Enter the row number: 2
Enter the column number: 1

What To Turn In.

A printed listing of the lab programs. Be sure that your name is in the program prologue’s comment section of the program.

Reminder.

Be sure to save a copy of each program on your P:\ drive.