Lab 4: Functions, Refactoring, Testing
Goals
After the lab, you should be proficient at
- using and defining functions within your program
- refactoring your code to use functions
Objective: Review
Review the slides for today.
Objective: Set Up
Run labhelp
As usual, create a directory for the programs and output you develop in this lab.
Copy all the .py files
in /csci/courses/cs111/handouts/lab4/ into
your lab4 directory.
Objective: Textbook and Pre-Lab Checkpoint
On your laptop or on the lab machine, log into the interactive textbook. Check that the grades for the pre labs (up through pre lab 3) are what you expect. If they aren't, please talk to me.
Select Practice under the User Profile to practice
some of the material you've learned so far. You don't need to
practice right now -- just know that that feature exists.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file. Name the files lab4_1.py, lab4_2.py, etc. Note the change in naming scheme.
Your programs will be graded on correctness, efficiency, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
After you've developed a correct solution to each program, close
reopen the IDLE "shell" by running the program again (using F5),
demonstrate that the program works using several good test
cases, and save the output to a file named lab4_x.out,
where x is the problem number.
- (20) Let's start by getting you comfortable with calling
functions that have been defined within the same file.
Open
lab4_1.py, which you copied earlier. Read through the functions and their documentation. Focus on the function names and their API (what is their input, what do they do, what do they return?). Then, modify themainfunction such that it implements what is described in its comments, e.g., draws and moves bugs, using functions defined within the program or methods from the Graphics library.There will be no saved output for this program.
- (15 pts) Let's try turning code that you wrote before into a
function--Refactoring!, so that we're focused on defining
functions rather than writing new code.
Open your
lab3/lab3.5.pyfile. Create a new file in this directory named aslab4_2.py. (Note the change in naming scheme.) Refactor your code such that the code that displays the first 20 Fibonacci numbers (and only that code) is in a function calleddisplayFibonacciSequence. This function should not take any instructions. Follow the instructions to refactor your code from the in-class handout. Then, copy your other code and put into a main function.Note that the programmatic testing will not work for this function because the function does not return anything.
Your
mainfunction will likely be quite short. - (20 pts) Now let's try writing a function that returns
something and programmatically testing that function.
Open a new file (which will be named
lab4_3.py).Copy your function
displayFibonacciSequenceinto this new file and name itgenerateFibonacciNumber. The function should take as a parameter the number of the Fibonacci sequence (which must be greater than 2) to generate and returns that number. Update the documentation accordingly.To help make the desired functionality clear, the following test case should pass:
test.testEqual( generateFibonacciNumber(6), 5 )Write a test function that tests that your function works correctly.
After you have verified that your tests work, comment out the call to your test function.
Now, copy the
mainfunction from the last program into this file. Change the function to prompt a user for which Fibonacci number they want and then display that Fibonacci number.A sample run is shown below:
This program computes Fibonacci numbers. Which Fibonacci number are you looking for (larger than 2)? 6 The Fibonacci number at position 6 in the sequence is 5(The output is a little awkward. We don't have a good way of saying the "6th" number vs the "1st" number, etc.)
- (15) We haven't been making clear the importance of reusable
functions because all of the functions we have defined have been
used only within the same program.
- Open
game.py. This file is the start of our owngamemodule that we will use in subsequent labs. - Implement the function
rollDie. You may need to review therandommodule. Remove the statementpass, which is just meant to make the module be able to execute while you're still developing. - Test the function using the
testRollDiefunction. - In comments at the top of the program (below the
high-level description), answer the following
questions: Why can't we use
test.testEqualto test this function? Why can Professor Sprenkle write a function that tests your code without knowing how your function is implemented? - Implement the function
rollMultipleDice, using another function that you implemented. - Uncomment the call to the
function
testRollMultipleDiceand test your new function. - When you're sure your program works, save the output from executing
both
the
testRollDieandtestRollMultipleDice(in one run). Save it in a file calledgame.outso that it's with the code in the printable lab.
If you run
yahtzee.py, you should see the output from rolling 5 dice. We can't do a whole lot more than that just yet, but hopefully, you could imagine that happening. - Open
- (20 pts) Overview: Write a program that
calculates a person's body mass index (BMI), given their weight and
height. The formula for calculating the BMI is weight (in pounds) /
[height (in inches)]2 x 703
Your program should define a function that takes as parameters the person's weight and height and returns the BMI.
In another function, test that function, using the
testEqualfunction.After verifying that your function works, comment out the call to the test function.
Then, create a
mainfunction. Your program should prompt the user for the weight (in pounds) and height (in inches) and display the BMI, rounded to 1 decimal place.
Finishing up: What to turn in for this lab
You have completed several labs. I am not as explicit in the instructions anymore. Refer to the links or previous labs to remind yourself of instructions/commands.
- Clean up: Move
graphics.pyinto yourcs111directory. (seemv. Note where thecs111directory is with respect to where you're running this command from.)
In other words, you should only have the.pyfiles you wrote,test.py,yahtzee.py, the.outfiles you created, and a__pycache__directory in your directory when you print. (The latter is not printed.) - Create your printable lab assignment, using
the
createPrintableLabcommand:
createPrintableLab <labdirname> - View your file using
the
evincecommand. It should only be a few pages. If it is longer than that, check if you havegraphics.pyin the PDF. Go back and follow the steps to move that file out of your lab directory. - Print your file from Evince. If you're in the Advanced Lab, use its printer.
- Move
graphics.pyback into yourlab4directory. - Submit your lab4 directory into
your
turnindirectory.
Print your lab before class. Submit the printed and electronic version before the beginning of Friday's class.
Grading (90 pts)
- Python programs: 90 pts; see above for breakdown