Technical Homework 6: Refactor Code to use Functions

In this assignment, you will “refactor” some of your previous assignments and labs to make them use functions. Specifically, you will refactor these assignments:

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.

  1. 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.

  2. 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 x position of the person
    • The y position 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.

  3. 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:

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