Week 5 Lab

Part 1: Practice with Boolean Functions

As the book describes, a boolean function is a function that returns a boolean value -- True or False. Creating these kinds of functions can make for some very readable code. E.g.:

def isOdd(val):
    """return True if val is odd, False otherwise"""
    if val % 2 == 1:
        return True
    else:
        return False


print("Is 37 odd? Let's find out: ")

if isOdd(37):
    print("Yes!")
else:
    print ("No. :-(")

Step 1: Make the lab5 folder

Open up Eclipse. Make sure you are using your workspace on the S: drive -- should be S:\workspace. Once your workspace is correct, select CS104, right-click and create a new folder called lab5. Then, right-click on that folder and create a new file called triangles.py.

Step 2: Project Goal

Write code that reads in three float values from the user and then prints out one of these responses:

The triangle is an equilateral.
The triangle is an isosceles.
The triangle is scalene.

So, e.g., a run of your program might look like this:

Enter length of side 1: 3
Enter length of side 2: 4
Enter length of side 3: 5
The triangle is scalene.

Step 3: Get started

Put this code into your file:


# top comment block here

# imports

# constants defined here

# functions defined here

# ----------------- main code ------------------------

Now, write code in the "main code" section to read in the 3 values from the user. Make sure you store the results in variables side1, side2, and side3, and make sure they are stored as floats.

Run the code to make sure you have no typos so far.

Step 4: Write a function to test for an equilateral triangle

You need to first write a function that takes the 3 sides as parameters and returns True if the sides are all the same length, and False otherwise.

Here is a start to the first function:

def isEquilateral(s1, s2, s3):
    """return True if s1, s2, and s3 are the same value and thus
represent an equilateral triangle.""" <your code here>

Put this function in the # functions defined here section of your file.

Now, we need to test your isEquilateral() function. In the main section, after your existing code that reads in the 3 values from the user, write an if-else statement. In the body of the if statement, print out "The triangle is equilateral." In the else part, just print something else out at this time -- like "Nope". See the example of code at the very top of this lab if you need help.

NOTE NOTE NOTE: remember you have to *call* your function like this:
if isEquilateral(side1, side2, side3):
Many student incorrectly try this, which does not *call* the function:
if isEquilateral == True:

NOTE NOTE NOTE: don't forget to make your function return False when the values are not all equal.

Step 5: Add functions to test for isosceles and scalene.

Now, copy and paste your isEquilateral function definition twice, calling the new functions isIsosceles and isScalene. Change the comment for each, and change the code to make the correct tests.

Now, change your main code to use an if-elif-elif statement so that it first checks if the sides represent an equilateral triangle, then test if the sides represent an isosolese triangle, and finally check if the sides represent a scalene triangle.

Step 6: Submit

Before you submit your code, make sure that this information is in a comment block at the top of the file triangles.py:

#
# CS104 Lab 5: <something here to describe this lab>
# <Your Name and your partner's name, if you have a partner>
# <Date>
#

To submit your final version of triangles.py, you’ll need to use Windows Explorer.

This lab is worth 5 points. 
3 points: program that runs correctly. 
1 points: correct comments at the top of the file.
1 points: code is clean and neat and perfectly indented, with appropriate comments.


Part 2: Approximating the Value of Pi

In this lab, you will...

Step 1: Find the lab.

Go to the online textbook that we use. In the Selection chapter, click on "Conditional Execution: Binary Selection". About half way down, you'll see a Lab called "Approximating Pi with Simulation". Click on that and do that lab. Work with a partner. NOTE: in the next step you have to copy your code from the browser environment into Eclipse and then submit from there. So, you could just work in Eclipse from the beginning if you want. If you are going to work in Eclipse, then follow the steps below now to make the lab5 folder and the file.

Step 2: Make the lab5 folder

Note that you do not submit your code in the online textbook. Our graders cannot see any code submitted there. Instead you need to copy your final code out of the browser there, and submit it like we normally do. Follow these steps:

Make a folder called lab5 (if you haven't already). Then, right-click on that folder and create a new file called montepi.py.

Copy your code out of your browser workspace and into montepi.py. Make sure you save the file there! Also, make sure you put this code at the top of the file:

Before you submit your code, make sure that this information is in a comment block at the top of the file montepi.py:

#
# CS104 Lab 5: <something here to describe this lab>
# <Your Name and your partner's name, if you have a partner>
# <Date>
#

To submit your final version of montepi.py, you’ll need to use Windows Explorer.

This lab is worth 5 points. 
3 points: program that runs correctly. 
1 points: correct comments at the top of the file.
1 points: code is clean and neat and perfectly indented, with appropriate comments.


Part 3: Better Turning Function


In this lab, you will ...

Step 1: Make the lab5.py file

In your lab5 folder (which you may have to create, if you haven't already), create a new file called lab5.py.

Now, do something different: go to your lab4 folder's lab4.py, and copy your code and paste that code into lab5.py. I recommend you then close your lab4.py file so that you aren't tempted to change it instead of lab5.py.

Now, delete the code in the main section, except for the init() call. Do not delete the function definitions you just pulled in.

NOTE: what we are doing is building up more and more nice useful functions for us to use to control the scribbler robot. So, we are going to keep turnLeftDegrees() and turnRightDegrees(), etc., and add a new function that uses these existing and well-tested functions.

Step 2: Think about the problem

Before you write any code, let's think about what we want: We want a more general and more robust turn function that will work for these cases:

Step 3: Slow start

This is a complicated function, so we'll start slow. First, comment out the init() call (we won't need to connect to a scribbler yet) and put this code in your file (in the "main" area):


a = 371
print(a, a % 360)
b = -1
print(b, b % 360)
c = 720
print(c, c % 360)

Run that code, look at the output, and make sure you understand why it is printing what it is printing. You really should not go on until you really understand what the % operator can do for you. NOTE NOTE NOTE: observe what happens to negative values when you "mod" by 360.

Step 4: Get rid of extra turns around the circle.

Create the turnDegrees() function definition in your code. It takes a single parameter, degs. Add a docstring below it that explains that it will turn the scribbler left or right, the correct # of degrees, with negative being turning to the right.

The first thing to do in the turnDegrees() function is to take your parameter degs and make sure its value is between 0 and 360. Anything greater than 360 is just going around the circle more than we need to, and anything negative can be done by turning a positive number of degrees.

Write code that does this, using % 360. You may store the result back in the degs variable.

Now, write an if statement that tests if degs is 0. If so, your function should just return -- there is nothing else for the function to do. The code looks like this:

    
    degs = <yourcode here using % 360>
    if <your code here to test if degs equals 0> :
        return

Step 5: Don't turn too far

So far the function takes its input in variable degs and makes sure the value is between 0 and 360 -- which, incidentally, is always a positive number. Then, the code checks if the value is 0, and if so, it does nothing. That's good progress.

Now, I suggest you handle the case where if the value is <= 180 you turn left. Add an elif to your code in turnDegrees() to handle the case where degs is less than or equal to 180. You'll handle this by calling the existing (and tested!) turnLeftDegrees(). Then, add similar code to handle the case where the value is > 180 (i.e., more than half way around to the left), and you turn right. Note that there is a little trick required for when you turn right -- draw a picture or two to figure out what you have to do...

Then, add code in your "main" part to test your new fancy turnDegrees() function, multiple times with multiple values. Make sure you print out a line that states what your code is testing (i.e., what the scribbler should do). E.g., you main code might start looking like this:

init("COM40")
print("Testing turning 45 degrees to the left")
turnDegrees(45)
print("Testing turning 90 degrees to the right")
turnDegrees(-90)

Note that you'll want add more tests here in the main code to make sure that your turnDegrees code is correct for all situations.

Step 6: Submit your code

Your main code consists of various calls to turnDegrees() that test if it is correct for various inputs. You will be graded on how thoroughly you test all the cases that need to be handled, and if your code handles these cases correctly.

Put your name, partner's name, etc., at the top of the file, as usual.

To submit your final version of lab5.py, you’ll need to use Windows Explorer.


This lab is worth 10 points. 

6 points: program that runs correctly. 
2 points: main code tests all possibilities for ways to use turnDegrees()
2 points: code is clean and neat and perfectly indented, uses good variable names, good parameter names, good comments, etc. I.e., it is hospitable.