CS 110 Lab Project 3
In this lab, you will practice encapsulating sets of Python steps into functions. We will warm up with simple functions to repeat the calculation exercises from last week. Then we will use parameters with functions to repeat the two graphics assignments from last week. We will conclude with a re-coding of your Picture program.
For each of the programs, I expect good variable names and good commenting practices. There should be comments for the program itself, and we should comment each function so that the comments describe what the function does in terms of its parameters (sometimes called the function's 'givens') and details any return value and any output performed by the function.
Problem 1: Calculation functions
The following simple functions are characterized by having some parameter and then calculating a result based on that parameter. In these programs, we then output the result with a print statement, but in the future, we will learn how to have our programs return a value for use downstream in the program. Each of these problems will involve the definition of two functions: our main() function, with no parameters, and our calculation function. Note that we still want the operation of these programs to behave the same as before, so the main() function will take this into account.
- In land.py, the calculation should be encapsulated in a function show_acres(sqfeet) where sqfeet is the parameter in the function definition and will be used to calculate and then print the number of acres. The main() function should be defined to retrieve the input of the total number of square feet and to then pass this value as an argument to show_acres().
- In distance.py, the calculation should be encapsulated in a function show_distance(time), where time is the parameter for the time and will be used in the function definition to calculate and then print the distance, assuming a speed of 60 miles per hour. The main() function will invoke the show_distance() function three times, passing it, in turn, values of 5, then 8, then 12, as the argument for time.
- In temp.py, the calculation is the conversion from Celsius to Fahrenheit, and so we will name the function show_fahrenheit(celsius), where celsius is the parameter representing the temperature in Celsius. The function definition will calculate the Fahrenheit temperature and then print it. The main() function will retrieve a Celsius value from the user and then pass it as argument to show_fahrenheit().
Problem 2: Graphics Functions
- For your circles.py program, we want to design a function that, when called, will draw exactly one circle. So the main() function, in this case, will create the graphics window, set up coordinate values for x and y (depending on your sequence progression), and will include 10 invocations of the newly created draw_circle() function. The draw_circle() function will probably have parameters for the window and for the x and y coordinates of the center of each circle. Depending on your sequence, and what changes between the circles, the parameters could also include a radius or a fill color. But if these do not change from one circle to the next, they would not be parameters.
- For the grid.py program, the most straightforward way to redesign for functions would be to create two functions: draw_row_red() and draw_row_black(), where the colors indicate the starting color for the row. These functions would also have parameters for the graphics window and the starting position x and y for the row. One of the things that makes these a bit different are that, within these functions, the location of the subsequent squares will be expressions determining offsets from the x parameter.
Problem 3: Picture.py redesign
Using what you have learned about functions and parameters, redesign your Picture.py program to use functions. The top level sequence should be encapsulated by a main() function that creates the graphics window, executes the sequence of newly created functions, and ends by calling getMouse() and close().
Clearly, each of your Picture programs are different, and so you will each approach this assignment in different ways. If your program consists of a set of disparate elements, then your functions may perform simple grouping of statements. If your program repeats some sequence of steps, then your functions may, with parameters to distinguish a 'starting point' for the element, be an abstraction for drawing that element, and then be invoked repeatedly from your main() function.
Because variables defined in one function are not visible to other functions (think about the Function Call Stack picture), the variable for the graphics window you create in main() will almost always be an argument to your element functions (and a parameter in each of those function definitions), so that these element functions can draw to the same window created in main().