CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 6 - Functions!


As usual, create two directories for today's class. Create a directory called lecture6 under activities, and a directory called lab6 under labs.


Modules and Functions

Modules are extensions of functionality for Python. They allow us to select new functionality that was previous unavailable to us. Two modules we will learn about today are math and turtle.

These modules define constants and functions. These functions allow for the abstraction of behavior, which can allow us to do some really cool things.


In-class Activity 1

Turtle Module

The turtle module allows us to draw things to the screen. Using the turtle module, write a program called square.py that uses the turtle module to draw a square to the screen.


Creating functions

Functions are a way for us to give a name to some block of code. Not only can this improve readability, but now we can re-use code much easier than copying and pasting.

Functions do change the order in which things get interpreted and executed.


In-class Activity 2

Writing your own Functions

Write a function that draws a square from the current location of the turtle object. Use this function to draw 4 squares on the window, as shown below.


Lab Assignment 6

BMI

Body mass index (BMI) is a measurement devised in the 1800's, as a way of analyzing an individuals body shape, as it relates to their height in weight. While the values and benefits of such analysis is hotly contested, generally speaking a "normal" BMI is in the range 18.5 to 25.

In a file called bmi.py, create a function bmi, for computing BMI using values when given metric values. This function should use input to get information from the user, and print the calculated BMI. The BMI of a person is computed by dividing a person's weight (in kilograms) by the square of the person's height (in meters).

Your functions should be properly commented, and should use meaningful names for values and simple statements to make the program more readable. Make sure to include a comment header which lists who the authors are, as well as the purpose of the file.


Submission

When you have finished, create a tar file of your lab6 directory. To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 6. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your file.

Do not forget to email your partner todays files!


Last modified: Mon Sep 9 10:36:29 EDT 2013