By the time you have completed this lab, you should be able to
See lab01, step 3, for instructions on bringing up IDLE.
Now, arrange your IDLE windows to the "usual starting point". By this, we mean
This particular arrangement is only a suggestion—you can actually arrange the windows any way you like. But, if you are new to programming, keeping things organized like this may help you remember what to do, because this is the way Prof. Conrad organizes things during lecture.
It will also be the "usual starting point" we refer to in the exercises, so it will be helpful to develop this habit.
The program at the link below contains three function definitions, and some test cases. From the comments you can see that there there are functions for three problems, along with test cases for those functions:
http://www.cs.ucsb.edu/~pconrad/cs5nm/08F/ex/ex05/ex05PhillConrad.py
Some of these functions are correct, and some are incorrect. To find out which, we can run the program and look at the test cases.
Copy and paste this program from the web into the untitled window, and run the program. When you save the program, save it with a name like this (substituting in your own name, and following the pattern.)
Once you save this and run it, you should get output similar to the following:
IDLE 1.2.2
>>> ================================ RESTART ================================
>>>
Test perimRect test 1 passed.
Test perimRect test 2 passed.
Test perimRect test 3 passed.
Test FtoC test 1 failed: expected 100 but I got 194.222222222
Test FtoC test 2 failed: expected 0 but I got 14.2222222222
Test FtoC test 3 failed: expected 20 but I got 50.2222222222
Test areaRect test 1 failed: expected 6 but I got -1
Test areaRect test 2 failed: expected 10 but I got -1
Test areaRect test 3 failed: expected 9 but I got -1
>>>
The output above indicates that the tests for the perimeter of a rectangle function pass, but the ones for other other two fail. Your next job in this exercise is to fix the two broken functions. We'll do that in the next step.
At the top of the file is a comment that looks like this:
# ex5PhillConrad.py Exercise 5 for CS5nm, Fall 2008, Phill Conrad, UCSB
# define several functions, and test them
Change this comment to have the correct name of your file, your name, and today's date.
Note: From now on, you should always do that in this course whenever you make changes to a file and submit it. This is an important part of documenting your program, and is one of the things you'll be graded on, so try to remember this. (For a while, I'll remind you, but after a few exercises, I'll expect you to have learned to do this without a reminder.)
The function definition for FtoC has an error in it. Use the internet to look up the correct formula, and change the return statement so that the function works correctly.
The function body for areaOfRectangle is currently just:
return -1 # stub
This is a special kind of function body. We call it a stub.
Some facts about stubs:
Using a stub allows us to "shake the ladder"—that is, to see if our safety systems are going to keep us safe. In this case, the "safety system" we are referring to is our set of "test cases".
Your job is to change this stub to a proper function body. To do that you'll replace the -1 with some expression involving length and width that actually computes the area of a rectangle (and remove the # stub comment.)
Do that now, so that the test cases pass.
Now, add a function definition of your own, along with some test cases for it.
The name of your function should be areaOfTriangle. The parameters to the function will be base and height. The formula is "one half of the base times the height" (you'll need to convert that into Python notation).
In addition to writing the function, write at least three test cases. For now, you can assume for now that the numbers passed in for base and height will be positive numbers, so you don't have to worry about "weird" things, like if the numbers passed in are negative or zero.
areaOfTriangle(5,10), then the number passed in for base is 5, and the number passed in for height is 10. We call 5 and 10 the actual parameters of the function call.You should come up with your own test cases.
You might like to start by writing a stub, and making sure the test cases fail. You can then fill in the body with a correct definition of the function so that the test cases pass.
When all your test cases pass, you are finished, and ready to submit.
Now, to make a transcript of your work, save all your work, and quit out of IDLE.
Go back into IDLE, and open up your saved file.
Run the file, so that we see the tests passing in your Python Shell Window.
Then, make a transcript file (just like you did in lab01), like this:
While you are in the IDLE window, not the window with your function defintions:
Submit the files ex5YourName.py and ex5.transcript.txt on GauchoSpace as your submission for Exercise ex05.
Then you are finished with this exercise!
Ex05 is due next Wednesday at 5pm. You should try to complete as much of them as you can during today's lab. If you cannot complete them, you can work on them during the week. If you have questions, come to office hours for help:
You can also ask questions during next week's lab, but ideally you should be finished or "almost finished" with both Ex05 when you come to lab next week.