In this assignment, you will “refactor” some of your previous assignments and labs to make them use functions. Specifically, you will refactor these assignments:
-
Lab 2C: Stick Figure
-
Homework 4: Estimating an Integral
Re-doing Lab 2C Drawing a Stick Figure ¶
Find the file you submitted for drawing a stick figure. This should have been
called stick_figure.py. Make a copy named crowd.py – you’ll see why.
Open this file in Thonny.
You need to do 3 things to get full credit.
-
Add 2 comments to the file:
# -------------- Functions -------------and
# -------------- Main code -------------The areas below these comments are where you will put your function definitions, and your main code, respectively.
-
Move all the code that actually draws the stick figure into a function called draw_person(), defined in the correct place in the file. You should pass in 4 parameters:
- The turtle object (
pen) to use - The
xposition of the person - The
yposition of the person - The size of the person
To test this step, call your function from your main code. The result should be unchanged from Lab 2.
- The turtle object (
-
Change your main code so that you draw 100 people, in random locations on the screen, and of random sizes (within reason). (If you had code to ask the user for the size of the person, remove that.)
Notes:
- It is OK if some people are partially off the screen.
- use
window.tracer(False)before drawing to turn off tracing so it draws faster. Turn the tracer back on (True) when you’re done to show the result.
Estimating an Integral (Homework 4) ¶
This one is easy: move as much code into functions as makes sense. One function should be computing the y value for a given x – i.e., doing the function. You should also have a function for drawing the axes, a function for drawing the curve, and a function for plotting the dots. You might find reasons to make even more functions.
Again, you should pass in all the parameters the function needs to do
its job. A function should not use global variables – except for
CONSTANTS.
Submit your two files, crowd.py and integration.py, to Moodle.
Grading Rubric ¶
10 pts total: 5 for each part
-
2 pts for correctness.
-
2 pt for making nice clean functions with good names, etc.
-
1 pt for a docstring for each function, a well-organized file (imports at the top, functions in the functions area, main code in the main code area).