CSCI 110, Spring 2011
Home | | Course Schedule | | Assignments | | Lecture NotesLaboratory 5
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:
Due at the end of today's class
Write a program, drawXorO.py, 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.
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, 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:
#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.
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
Problem 3.
Write a program named gradeAvg.py, to compute the average grade for a set of test
scores. The program should prompt the user for the number of test scores to enter and
read in that number. It should then call a function, GradeAverage(), which will
return a numeric value. The GradeAverage() function will use a for loop to prompt the
user to enter the specified number of test scores. As each score is entered, that score
should be added to a running sum of the scores. After all the scores are entered (and
their sum is complete) the function should return the average of the scores (the sum
divided by the number of scores). GradeAverage() should have one integer
parameter (the number of scores to be read in and averaged) and should return a floating point
numeric value (the average). It should have 3 local variables: an integer variable to serve as a
counter for the for loop, a real variable to read in each score, and a second real variable
to keep track of the sum of scores.
Note: If all the scores entered are integers, you will need to cast the sum to a floating point number before computing the average (or Python will use integer division and truncate the decimal). To cast a variable to floating point, use the float( ) function. E.g. if the variable is named sum, the cast would be:
You may use the program skeleton given below:
#Program: gradeAvg.py #your name and program description goes here. #your definition for GradeAverage function goes here numScores = input("How many scores do you want to enter? ") average = GradeAverage(numScores) print "The average score is %6.2f"%average
A sample session for the problem might look like the following:
How many scores do you want to enter? 3 Enter test score: 90 Enter test score: 91 Enter test score: 91 The average score is 90.67
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