Introduction

The chart shows the level of excitement growing when the anticipation of chocolate covered candy is introduced but interest drops sharply upon mentioning it is raisins.
The chart shows the level of excitement growing when the anticipation of chocolate covered candy is introduced but interest drops sharply upon mentioning it is raisins. © Justin Haplern

In this module you'll explore more about how to use functions, functions that return values, unit testing, global and local variables, the Accumulator Pattern, functions that call other functions, the flow of execution, the main function, composition, and how to draw a bar chart.

In class, you'll update your Python Concept Reference Sheet, draw colored shapes using math, and update the VS Code settings.

You'll also learn how to access external data sources.

Then, you'll create your own bar chart using an existing external data file.

Read Think Python Ch 6 ~ Python Functions

Read through Think Python's Chapter 6 Functions. :

Read Think Python Ch 11 ~ Python Functions

Read through Think Python's Chapter 11 ~ Files. :

In-class Activities

Update your Python Concept Reference Sheet

In your existing small group, collaborate in your existing Google Sheets or Excel file to describe the new Functions presented in chapter 6 and File actions as presented in Chapter 11. Continue to add to the list as you progress through the chapters.

Draw Colored Shapes using Functions within Functions

Four circles drawn with python functions.
Four Python turtles each with a different size, position, and speed drew circles using pi and radius.

Work together in small groups to create 4 circles (using pi) drawn by four different turtles, each starting in a different position on canvas, and each having different radii, fillcolor, pencolor, and speeds, using functions within functions. Use these specifications and limitations:

Refer to Lessons from a Triangle for related functions.

  1. Define a polygon function.
  2. Define a circle function (not the built-in circle) using pi and radius
  3. Base the circle function on the polygon function.
  4. Define a main() function for the window, turtles, and drawing.
    • Size the canvas to 400 by 400 and position it at position 0, 0.
    • Define 4 turtles, each with their own starting position, pensize, and speed.
    • When calling the drawing function, pass-in the turtle name, color, and size.
    • The turtles can draw one at a time or simultaneously.
    • Click to close the window.
  5. Circles must remain within the 400 by 400 canvas.

Vocabulary

Diagram showing radius, diameter, and circumference of a circle, with calculations.
Diagram showing radius, diameter, and circumference of a circle, with calculations.
Angle
The shape formed by two rays or lines eminating from a common point.
Radius
A line segment from the center of a circle to its perimeter boundary.
Diameter
The distance between one side of a circle to its other side, passing through its center.
Circumference
The length of the permimeter of a circle.
π pi
A mathematical constant that is the ratio of a circle's circumference to its diameter.

Update VS Code Settings

VS Code Settings for execute in file directory.
Activate VS Code's Setting for "execute in file directory", so that external files will work with your programs.

So that we can run external data files within Visual Studio Code using Python, update the app Settings:

  1. In VS Code, go to the File (Windows) or Code menu to choose PreferencesSettings.
  2. In the Search bar at the top, type execute In File Dir
  3. Click the box next to the setting to activate it.

Write Program 5

Update VS Code Settings

VS Code Settings for execute in file directory.
Activate VS Code's Setting for "execute in file directory", so that external files will work with your programs.

So that we can run external data files within Visual Studio Code using Python, update the app Settings:

  1. In VS Code, go to the File (Windows) or Code (Mac) menu to choose PreferencesSettings.
  2. In the Search bar at the top, type execute In File Dir.
  3. Click the box next to the setting to activate it.

Limitations

  1. Do not copy any of the following from Ch 6.11 or from the in-class demo:
    • variable names.
    • def names (but do use main(); it is an expected function name).
    • values for padding, pensize, pen or fill colors, font-family, font-weight, font-style, title positions, or widths.
  2. The title and chart must stay within the bounds of the 400x400 canvas.
  3. Colors must be easy on the eyes as well as easy to read...not too big, too small, or too bright.

Write the program

Turtle canvas with window title, chart title, and bar chart. Print of the data file values, highest temperature, and number of temperatures.
Example output of the Program 5's window title, chart title, and bar chart using the Turtle module, as well as print of the data file values, highest temperature and number of temperatures.
  1. In the sixth module folder of your workspace folder, make a new Folder named p5-username-temperatures. In that folder, make a new file named p5-username-temperatures.py. Replace 'username' with your ONID username.
  2. In the file, write commands in Python to do the following:
    1. Comment your first and last name and the date.
    2. Comment the name of this assignment "Temperatures".
    3. Comment a paragraph that describes the data, functions, and output of the program.
    4. Include the following:
      1. Pseudocode and docstring comments that describe each step of the program and its parameters.
      2. A function for writing the title of the chart in a large, bold font of a legible color: Benton County, Oregon Average Temps 2012 to 2022. Include hide, pen up and down, position, color, text, and font features.
      3. A function that draws a filled rectangle at the correct height in integers based on the temperature, and prints the temperature at the top of the bar. Include fill, directions, conversion to int() and str(), and a readable font.
      4. A main() function that does the following:
        1. Data File: Opens, reads, splits, and prints the benton-temps.txt file, which is a list of average temperatures from Benton County, Oregon between 2012 and 2021, from the Northwest Alliance for Computational Science & Engineering.

          Right-click to Save as... the .txt file to your workspace folder.
          • You'll need to use this with... statement of open, read, split, and print of the data file: with open('benton-temps.txt', 'r') as data: lines = data.read(); temperatures = lines.split()
        2. Gets and prints the max() and len() of the data file.
        3. Set a Turtle canvas at the top left corner of the screen in subtle color with a padding of 10 to give the bars some breathing room.
        4. Displays your first and last name in the canvas Title bar.
        5. Defines a constant variable for padding, to keep the chart from touching the edges of the canvas.
        6. Uses setwordcoordinates() to start the bar chart drawing at the bottom left corner and accounts for the padding when drawing height and bars.
        7. Defines a turtle that has a pen color, pensize, and fill color.
        8. Calls the title function.
        9. Calls the drawing function.
        10. Allow the window to close by clicking.
        11. Closes the data file.
      5. Calls the main function.
  3. Save the file using cntls or s.
  4. Run the program using one of these methods:
    • Click the run arrow ▶ at the top right of the editing screen.
    • In the terminal, type: python p1-username-datatypes.py
    • Right-click on the file in the file list (or from in the file) and choose Run Python file in terminal.
  5. Take a single cropped screenshot of the final output of your program. This will require you to move the chart canvas window to just above to the terminal/console printed statements (like my example above).
    • Windows:
      Windows Snipping Tool takes screenshots. Start → All Programs → Accessories → Snipping Tool New → Rectangular Snip to select just the portion of the window and screen you need to show. Or, newer Windows computer systems may use the Snip and Sketch tool's WindowsShifts keystroke. Save the file as .png and move it to your module folder.
    • MacOS:
      Shift4 to draw a marquee around just the canvas/window of your app. Or use the Preview application to capture, edit, and save the file. Apple Preview App takes screenshots Applications → Preview → File → Take Screenshot → From Selection. Drag the crosshair cursor over the area you want to capture. The image saves in .png format. Edit as needed.
      Or, you can use the searchsearch bar on the top right of the Finder to search "screenshot." The screenshot crosshair cursor will pop up automatically. Select the area you want to capture and it will save a file to the Desktop. Move the resulting .png file to your module folder.
    • Chromebook:
      CtrlShiftOverview: The screenshot crosshair cursor will pop up, so you can select just what is needed. Move the resulting .png file to your module folder.
  6. Backup your work to a cloud drive and/or USB stick drive.

Submit Program 5

Zip a single folder that includes all three files.

Place the p5-username-screenshot.png into the same folder as your p5-username-temperatures.py file and .zip them together. Upload the .zip archive to the Module > Assignment in Canvas. The Instructor/TA will download the zip archive and run your .py file to score it.

Respond to Feedback

Within 3 days of your submission, check the CanvasGrades area to view your score in the Rubric, along with our feedback.

Respond to the feedback if you have questions, by replying in the GradesAssignmentComment box.