MCS-177 Project 1: Let's Get Started! (Spring 2016)

Start: Tuesday, 2/16; Due: Monday, 2/22, by the beginning of class


Overview

This project is designed to reinforce some of the concepts we have learned from Chapter 1 of our textbook. I recommend for you to read Chapter 1 before getting started.

You are to do this project individually.

Task 1

The best way to learn Python is to try it out; so let's get started. The first thing you are going to do is to start the Python interpreter. The program we are going to use to introduce Python is called IDLE - named after Eric Idle of the British surreal comedy group Monty Python.

IDLE is already installed on the lab computers. If you want to install IDLE on your own laptop, please follow the instructions on the links page. You can start an IDLE session by clicking on the icon; the Python shell should now appear.

In the IDLE session, write and evaluate Python expressions corresponding to the following statements:

  1. The sum of the first 7 positive integers starting from 1.
  2. The average of 65, 57, and 45.
  3. Two to the twentieth power.
  4. The number of times 61 goes into 4356 (hint: the answer is an integer).
  5. The remainder when 4356 is divided by 61.
We have provided you with a template file. After you have finished typing the expressions and have gotten the answers, you should double check that your answers are correct. Then, cut and paste the expressions and the answers you received underneath the corresponding questions (Task 1, questions 1 - 5) in the template.

Task 2

In the IDLE session (you can start a new IDLE session if you want, or, if you have a session already opened, you can continue using that one), write and evaluate Python assignment statements corresponding to the following statements:
  1. Assign 6 to variable a and 7 to variable b.
  2. Assign to variable c the average of variable a and variable b.
  3. Assign to variables first, middle and last the strings "Louis", "Lei", and "Yu".
  4. Assign to variable fullname the result of concatenating string variables first, middle, and last. After the assignment, the variable fullname should have the value "LouisLeiYu".
After you have finished typing and evaluating the assignment statements, please cut and paste them underneath the corresponding questions (Task 2, questions 1 - 4) in the template.

Task 3

You found a summer job mowing the lawns in your neighborhood. You would like to know how long it will take for you to mow each one of them. You can find out the dimension of each lawn, and you know that your mowing speed is two square feet per second. Finally, you know that all the lawns in your neighborhood are rectangular.

Up until now, you have been typing commands in the Python shell. For this task, you are going to write functions in a new file window and then call them in the Python shell.

Please note that all the variable names you use in your functions should be descriptive.

  1. Open up a new file window (go to File, then go to New Window) and create a function called timeToMow. This function should have two parameters, one for the depth of a lawn (in feet) and one for the width of a lawn (in feet). Your functions should return the time (in hours) it will take to mow a lawn of the corresponding depth and width. Save your program in a file called lawn.py.

    Please be cautious of two things, first, there is a difference between a function that returns an answer and a function that simply prints it out. (Professor Yu should have talked about this in class, if you are still not sure, ask professor Yu or look at your lecture notes.) Second, for this part of the task the time it takes for you to mow a lawn can be a floating point number. (For example, taking 2.3 hours to mow a lawn is completely fine.)

  2. Now that you have created your function, you can run your program by using IDLE's "Run Module" command from the Run menu; or equivalently by pressing the F5 key. If IDLE reports any syntax errors in your program, try fixing them. When you get past that hurdle, use the Python shell to try your timeToMow function out.

    Specifically, use the timeToMow function to calculate how much time it would take to mow a lawn that is 120 feet in depth and 210 feet in width. Make sure that your function gives the correct answer; if not, fix it. Cut and paste the input (the command you typed in the Python shell calling the timeToMow function) and the output (showing how long it takes to mow a lawn that is 120 feet in depth and 210 feet in width) from the Python shell underneath the corresponding question (Task 3, question 2) in the template.

  3. Suppose that you are only given the depth of a lawn, but you know that all the lawns in the neighborhood have the depth-to-width ratio of 9 to 16. In the same file (lawn.py), create another function called roundedTimeToMow. This function should have only one parameter, the depth of a lawn (in feet), and return the time it will take for you to mow a lawn of the corresponding depth rounded to the nearest hour. Your definition of roundedTimeToMow should call the timeToMow function you previously defined, so that if you were ever to adjust timeToMow to reflect a different mowing speed, roundedTimeToMow would automatically also start using the new speed.

    For this task, the time it will take to mow a lawn (in hours) can not be a floating point number. Instead, your function should return an answer that is rounded off to the nearest integer. Python has a function called round for doing the rounding. For example, round(2.4) evaluates to 2, whereas round(2.6) evaluates to 3. Insert an appropriate invocation of the round function so that the answer returned from your roundedTimeToMow function is guaranteed to be an integer.

  4. Run your program again. This time, use roundedTimeToMow to calculate the time it will take to mow a lawn that is 100 feet in depth. Make sure that your function gives the correct answer; if not, fix it. Cut and paste the input (the command you typed in the Python shell calling the roundedTimeToMow function) and the output (showing how long it takes to mow a lawn that is 100 feet in depth) from the Python shell underneath the corresponding question (Task 3, question 4) in the template.
  5. Finally, you are to construct a table showing the time it takes to mow lawns of increasing depth (given that all the lawns in the neighborhood have the depth-to-width ratio of 9 to 16). The first column of the table should show the depth of lawns ranging from 100 feet up to, but not including, 200 feet, stepping by 10 feet each row. The second column of the table should show the corresponding time it takes to mow a lawn of this depth. You should use a for loop and the roundedTimeToMow function to construct this table.

    Please be cautious of two things, first, you should use roundedTimeToMow to calculate how long it will take to mow a lawn of particular depth. Second, you should use a for loop to print out the table. You can do this by typing a for loop in the Python shell or by writing another function in lawn.py consisting of a for loop. Be sure to check your result. Cut and paste the input (the command you typed in the Python shell, which may just be running a function you wrote in lawn.py". The function definition, if there is one, is going to be submitted in lawn.py and hence doesn't need to be in the project1a.txt file.) and output (your table) underneath the corresponding question (Task 3, question 5) in the template.

Extra Credit

After you completed Task 1 - 3 of your project, you can attempt these questions for an additional 3 points. First, you should read pages 37 - 41 of your textbook. Then, you are to write Python codes that draws the pictures below using Turtle graphics.

                    example1

              example2

Note: for these two pictures, the size of each polygon is not important (you can make them any sizes). You must also use a for loop to draw each of the polygons.

example3

Note: the size of the three circles is not important either; their centers should be, more or less, the points of an equilateral triangle...

There are several ways to draw these pictures. We recommend the following method:

  1. Define a function that draws polygons of any sides and sizes.
  2. Define another function that draws circles of any radius (for references, see the drawPolygon and drawCircle functions on page 38 and 40 of your textbook).
  3. Finally, call your functions in the Python shell.
After you had finished drawing these three pictures, copy and paste the following into a word document:
  1. Any functions you created in order to draw the polygons and the circles.
  2. The commands (input) you typed in the Python shell.
  3. The resulting pictures drawn (you can take screen shots of the output).
Save your word document as extraCredit.doc.

Hint: for the last picture, you will need to use other functions from the cTurtle library, ones that we will not mention in class. Look at Appendix C of your textbook for various function from the cTurtle library and try them out (if you have a second-edition textbook, your Appendix C describes the similar turtle library instead. You will have fewer difficulties if you use the cTurtle library instead). If you are stuck, ask your lab instructor for help.

Submitting Your Work

You will be submitting your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following 2 - 3 files (with these recommended names):

Grading

You will earn one point for each of the following accomplishments. A minor error results in a 1/2 deduction.

  1. You gave the correct python expression for Task 1 question 1.

  2. You gave the correct python expression for Task 1 question 2.

  3. You gave the correct python expression for Task 1 question 3.

  4. You gave the correct python expression for Task 1 question 4.

  5. You gave the correct python expression for Task 1 question 5.

  6. You gave the correct assignment statement for Task 2 question 1.

  7. You gave the correct assignment statement for Task 2 question 2.

  8. You gave the correct assignment statement for Task 2 question 3.

  9. You gave the correct assignment statement for Task 2 question 4.

  10. You used the correct notation to define a function called timeToMow. The function has two parameters.

  11. Your timeToMow function correctly calculates the time it will take to mow a lawn of certain depth and width. Your timeToMow function returns an answer.

  12. Your input and output for Task 3 number 2 are correct.

  13. You used the correct notation to define a function called roundedTimeToMow. The function has one parameter.

  14. Your roundedTimeToMow function use the one parameter to correctly calculate the width of a lawn.

  15. Your roundedTimeToMow use the timeToMow function to correctly calculate the exact time to mow a lawn.

  16. Your roundedTimeToMow function returns an answer and correctly rounds the returned answer to an integer.

  17. Your input and output for Task 3 number 4 are correct.

  18. For Task 3 number 5, you used a for loop to construct the table. The starting size, ending size, and step size of your for loop are correct.

  19. For Task 3 number 5, the body of your for loop uses roundedTimeToMow.

  20. For Task 3 number 5, the body of your for loop prints two columns of correct data.

  21. Extra credit: you wrote the correct code to draw picture # 1 (the 5 sided polygon); a 1/2 point deduction for a minor error (such as not using a for loop in your implementation or not including the input and output)

  22. Extra credit: you wrote the correct code to draw picture # 2 (the 7 sided polygon); a 1/2 point deduction for a minor error (such as not using a for loop in your implementation or not including the input and output)

  23. Extra credit: you wrote the correct code to draw picture # 3 (the 3 circles); a 1/2 point deduction for any minor error (such as not including the input and output)