CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 17 - Testing and Turtle


As usual, create two directories for today's class. Create a directory called lecture17 under activities, and a directory called lab17 under labs.


Pop-quiz!

Answer the questions in the pop-quiz file. You have 15 minutes. Follow the instructions carefully. I will not be answering any questions during the quiz.


In-class Activity 1

Perfect Numbers

A perfect number is a positive integer that is the sum of all of its perfect divisors, excluding itself. Write a program perfect_numbers.py in your lecture17 directory, which prints all of the perfect numbers less than 1000.

You will want to create two functions: is_divisor and is_perfect_number. is_divisor should return True if and only if parameter y divides x evenly. It should return False in every other case. is_perfect_number should use a for loop to go through all the numbers less than parameter n, and accumulate the sum of all values that divide n evenly. It should return True if and only if n equals the accumulated sum. It should return False otherwise.


Testing

Up to this point, you have simply been asking me if your programs look correct. While this is fine for the time being, it would be more beneficial for you if you could justify to yourself that your code is correct. One form of testing code is known as boundary testing. Where are the boundaries in our code? How can we use this information to create proper test cases for our programs?


In-class Activity 2

Creating and Executing Test Cases

The file test_me.py contains a simple implementation for date validation, that does not use functions well. Download this file into your lecture17 directory, and add code at the bottom of the file that will test the code provided.

For each test case written, put a comment after the test case that indicated whether or not the test case was successful. Are there any problems in validate_date? Do not fix the problems, just indicate in your comments where they are.


Turtle Objects

Thus far, we have been using functions out of the turtle module. This, unfortunately, limits us to only a single turtle. While you can force this to work, like some of you did for the hunter and the monkey extra credit, it's a pain sometimes to do this. However, we can actually make more than one turtle, by creating turtle objects.

Once we have turtle objects, we can use methods to extract information about the turtle, as well as commanding a particular turtle.


Lab Assignment 17

Random Walking Turtle

One cool thing about turtle is that we can use .gif images to change what the turtle looks like. Search google image search to find an interesting (but class appropriate) .gif image. Download this image into your cs120/labs/lab17 directory.

Create a file called random_walk.py, and use the turtle.addshape function to add your gif to the turtle environment. addshape does not change the turtle to your shape. Instead, it loads the gif into the program, so you can change the shape of a particular turtle.

Create a turtle object called image_turtle, as described in class. Use the shape method to change the shape of the turtle. Notice that this is a method, not a function. The only difference between a function and a method is that a method executes on some object. So instead of turtle.shape, you would write image_turtle.shape, with the necessary parameters.

Add two constants to the top of your file, which represent the width and height of the image you changed the shape of the turtle to. You will use this later.

Use the random module to make the image_turtle randomly move around the turtle screen. Keep in mind that we are now using the methods on the image_turtle, as opposed to the functions in the turtle module. So your movement commands should be image_turtle. then the method name.

Challenge: Add code to your program that makes sure that your randomly walking turtle will never leave the edge of the turtle screen.


Submission

When you have finished, create a tar file of your lab17 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czf lab17.tgz lab17/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 17. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your file.

Do not forget to email your partner todays files!


Last modified: Thu Oct 3 15:46:00 EDT 2013