MONT 105S, Spring 2009
Home | | Course Schedule | | Assignments | | Lecture NotesLaboratory 6
Problem 1:
The following code can draw an X:
The following code can draw an O:
You should use the following template for your program:
The following is a sample output of the program:
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 2:
What To Turn In. A printed listing of the lab programs.
Be sure that your name is in the program prologues comment section of the program.
Reminder. Be sure to save a copy of
each program on your
Due at the end of today's class
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.
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)
yertle.up( )
yertle.goto(-50, -70)
yertle.down( )
yertle.circle(20)
#Program: drawXorO.py
#Include your program prologue here
from turtle import Pen as Turtle
yertle = Turtle( )
def drawX( ):
#Write your code to draw an X here
def drawO( ):
#Write your code to draw an O here
#Start the main program here
#Ask the user if they would like to draw an X or an O
#If they type X, call the drawX( ) function.
#If they type O, call the drawO( ) function.
Sample output 1:
Do you want to draw and X or an O? X
yertle.goto(-70+col*50, -30+row*50)
yertle.goto(-50 + col*50, -70 + row*50)
#Program: drawXorOposition.py
#Include your program prologue here
from turtle import Pen as Turtle
yertle = Turtle( )
def drawX(row, col):
#Write your code to draw an X here
def drawO(row, col):
#Write your code to draw an O here
#Start the main program here
#Ask the user if they would like to draw an X or an O
#Ask the user to enter the row number
#Ask the user to enter the column number
#If they type X, call the drawX( ) function
# to draw an X in the correct row and column position.
#If they type O, call the drawO( ) function
# to draw an O in the correct row and column position.
Sample output 1:
Do you want to draw and X or an O? O
Enter the row number: 0
Enter the column number: 2
Do you want to draw and X or an O? X
Enter the row number: 2
Enter the column number: 1