CS8, 09M, UCSB
H02: (Based on Miller/Ranum, Chapter 1 and Chapter 2) Total points: ? (printable PDF)
Accepted: on paper, in lecture (11am-12:25pm, Harold Frank Hall 1132 (note room change) ) on Tuesday, August 11th
No email submission allowed.
Name: (3 pts)______________________________ UCSBNetID (3 pts) _____________________
Lab Section (2 pts) Circle one: Thu 11am Thu 12:30pm Unknown
Circle the one you plan to attend, which may or may not be the one you are registered for.
To answer the questions on this homework, it will be very helpful to have a computer system running Python 3.0 or Python 3.1 available to you. To find such as system, you can either:
- log on to one of the computers in the CSIL computer lab, and access Python 3.0 or Python 3.1 there
- download Python 3.0 or Python 3.1 to your PC or Mac, and access Python 3.0 or Python 3.1 there
You can find more information about how to access these systems on the course web site under the "topics" link.
Review pages
- Review pages
29-42(the remainder of Chapter 1), then answer these questions:
- (20 pts) Write a function definition in Python. The name of the function is drawRectangle.
The function takes three parameters. The first is called t, and will stand for the name of a turtle. The second is width, and will stand for how wide the rectangle is. The first is called height, and will stand for how tall the rectangle is.
Assume that the turtle starts by facing "east", at the lower left corner of the rectangle. That is, the turtle will first draw the "bottom" line of the rectangle across, then draw "up" the right side, then back across the "top" of the rectangle, and finally, back down the left side.
(10 points for getting the first line of this function definition correct... the other ten points are for the rest of the function. If you aren't sure how to write the rest of the function, at least try to get the first line.)
Please turn over for more problems
Continued from other side
- Read about the range statement on pages 33 and 34, and review what we did with print(i) in lecture last week. For example, we showed that:
| For example, we showed that this code: |
Results in this output: |
for i in range(3):
print (i)
|
0
1
2
|
With that in mind, answer these questions.
- (2 pts) What two lines of python will result in the the sequences of numbers 3,6,9,12, etc. up to 30, being printed, one per line?
- (2 pts) (For this one, you may need to experiment with the actual Python prompt to discover the answer.)
What will be printed by:
for in range(5,1,-1):
print(i)
- (2 pts) Given what you learned in the answer to part (b) above, what is the Python code to print the sequence 20,18,16,...,2 (i.e. even numbers going down by 2 each time, ending with 2)?
- Read pages 45-82 about various ways to approximate the number pi. Then answer these questions:
- (4 pts) What is the purpose of typing import math at the top of a Python file, or as the first command in a session in Python?
- (4 pts) If you have assigned a floating point value to the variable x, what do you type at the Python prompt to display the square root of x?
- (4 pts) In the code in Listing 2.3 on p. 58 of the textbook, there is a line acc = 0.0
Obviously, this line assigned the variable acc to the value zero (as a floating point number).
What I want you to tell me is why? In the context of this program, why are we setting acc to zero?
- (4 pts) (Continued from the previous problem). We set acc to zero at the start of listing 2.3.
At the end of the function, we see the statement return acc.
Will the value of acc still be zero at that point? If not, explain what value acc will have. (You'll need to read the text around the listing to learn this.)
End of H02