Lecture 15 - Conditional Execution


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


Boolean Expressions

Up to now, you have used three different data types: int, float, and string. Today, we will learn about a new data type, boolean.


Conditionals

Conditionals allow us to change how our program executes, based off other values we compute. They use boolean values to determine whether or not to execute the next block of code.


In-class Activity 1

Rock, Paper, Scissors

Create a file called RPS.py in your lecture15 directory. Your program should generate a random play for the computer, and prompt the user to enter their play. Your program should display who won, and why.

Enter your play (r, p, or s): r
The computer played scissors
You Won! Rock beats Scissors

Lab Assignment 15

Points in Circle

Create a file in_circle.py in your lab15 directory. Write a function called is_point_in_circle that takes as parameters an x, y coordinate, and a radius r. This function should return True if and only if the point specified is inside a circle centered at (0, 0), with the specified radius.

Write a program that fills as much of the turtle window as possible with a circle. Your program should then compute A LOT of random points within the turtle window. For each point generated, use your is_in_circle function to decide if the specified point is in the circle. Use turtle.dot to draw a dot at the location specified. Keep a running tally of the number of points that you generated were in the circle.

Use turtle.write to draw how many dots were in the circle at (0, 0).

CHALLENGE: This mechanism can be used to estimate π, by comparing the area of the inscribed circle to the bounding square. Derive the equation necessary to estimate π, and perform your computations.


Submission

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

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

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 15. 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: Mon Sep 30 13:48:42 EDT 2013