Here is an updated version of the textbook chapters, and which exams cover them.
The entries that are in light grey italics indicate that the material is covered, but not emphasized
Chapter |
Midterm 1 | Midterm 2 | Final Exam |
|---|---|---|---|
1: The way of the program |
Entire chapter | Entire chapter | Entire Chapter |
2: Variables, Expressions, Statements |
Entire chapter | Entire chapter | Entire Chapter |
3: Functions |
3.1–3.7 | 3.1–3.7 3.8–end |
Entire Chapter |
4: TurtleWorld |
Entire Chapter | Entire Chapter | |
5: Conditions and Recursion |
5.1–5.7 | 5.1–5.7 5.8–end |
|
6: Fruitful Functions |
6.1–6.4 | 6.1–6.4 6.5–end |
|
7: Iteration |
Entire Chapter | ||
8: Strings |
8.1, 8.2, 8.4 | Entire Chapter | |
9: Case Study, Word Play |
Entire Chapter | ||
10: Lists |
10.1, 10.2, 10.5, 10.6 (append and sort only) |
Entire Chapter | |
11: Dictionaries |
|||
12: Tuples |
12.1, 12.2, 12.3 | Entire Chapter |
printBothTwice() that takes two parameters, "msg1" and "msg2", concatenates them together, and then prints that twice.
GoGauchos
GoGauchos
Monty Python
Python Monty
If I ask you to write function definitions using TurtleWorld, I'll provide you with this brief guide on the exam itself:
fd(t, distance) moves the turtle t forward by distance, and bk(t,distance) moves the turtle t backwards by distancepu(t) picks up the pen, and pd(t) puts down the penrt(t) and lt(t) turn right and left by 90 degreesrt(t,angle) and lt(t,angle) turn the turtle right or left by the number of degrees in angle.bob, you write:
world = TurtleWorld() # this is only needed once per program
bob = Turtle() # only needed once per turtle
Note:
from TurtleWorld import * or the sys.append("blah blah blah") and you don't need to put that stuff in your answers. Don't waste time writing that stuff on the exam—it will waste your time and not earn you any points!These problems are based on the figures shown. Be sure to include comments in these function definitions where appropriate.
| Figure 1 | Figure 2 | Figure 3 |
|---|---|---|
![]() |
![]() |
![]() |
equalTri(t,length) that takes a turtle as a parameter, and the length of one side of an equilateral triangle. Assuming that the turtle is facing "east" (towards the right hand side of the monitor when you start),
draw an equilateral triangle that points up (the bottom is flat and the top points towards the top of the monitor). The triangle's bottom left point should be the place where the turtle starts and ends. After the function call the turtle should be facing the same direction that it started from, as shown in Figure 1 above:
drawCircle(t,r) that takes a turtle and the radius of a circle, and draws a circle. Assume that if the turtle starts out facing east, he will finish facing east, seated at the very bottom of the circle. Using that function, create a function drawSnowman(t,h), where h is the height of the snowman. The snowman should be structured exactly at in ex09, according to the following diagram. Be sure the turtle starts and finishes, facing east, at the bottom of the snowman (at the point indicated in the diagram below.)
isElectionYear()
isElectionYear() consumes a single integer, which represents a yearTrue is the year is a US presidential election year, otherwise it returns False.check_expect() function, write two test cases for the function isElectionYear()—one that should return True, and another that should return False.isElectionYear(), but ten test cases is probably more than we need."
Fill in the blanks with what IDLE would print in the cases below.
>>> windy = True >>> raining = False >>> windy and raining _____ >>> windy or raining _____ >>> not windy and raining _____ >>> windy and not raining _____ >>> not (windy or raining) _____ >>> not windy and not raining _____ >>> not (windy or not raining) _____ >>> windy and (not raining) _____ >>>
Rewrite each of these expressions to remove the word not, without changing the meaning of the expression. The first two are done for you as an example:
| before | after |
|---|---|
not (a < b) |
a >= b |
a > 5 and not b == 10 |
a > 5 and b != 10 |
not( (a < 5) and (b == 10) ) |
|
(not (a < 5))and (b ==10) |
|
a <=5 or not b != 10 |
|
not (a==5 or b==10) |
For these problems, you also need material from section 8.4 on string slices.
(Note: Chapter 7 is not covered on Midterm 2)
Fill in the blanks from this IDLE session
>>> beach = "Rincon"
>>> beach[0]
________
>>> beach[-1]
________
>>> beach[2:4]
________
>>> beach[3:5]
________
>>> beach[3:3]
________
>>>
(Note: Chapter 9 is not covered on Midterm 2)
Fill in the blanks from this IDLE session
>>> primes = [2,3,5,7,11,13] >>> primes[0] ______ >>> primes[3] ______ >>> primes[5:] ______ >>> primes[11:] ______ >>> primes[1:4] ______ >>> primes.append(13) >>> primes[-1] ______ >>>
(Note: Chapter 11 is not covered on Midterm 2)
Fill in the blanks in the following IDLE session
>>> (x,y,z) = (1,2,3) >>> x _______ >>> z ______ >>> (y,z) ______ >>> coins = (1,2,5,10,25) >>> coins[3:4] ______ >>> coins[4:6] ______ >>> coins[2:4] ______ >>> coins[:1] ______ >>> coins[9:] ______ >>> coins[1:] ______ >>> coins[4] ______ >>> coins[-1] ______ >>>
A combined session on strings, lists and tuples:
>>> x = (1,2,3) >>> y = [1,2,3] >>> z = '123' >>> x[1] ______ >>> y[2] ______ >>> z[0] ______ >>> type(x) ______ >>> type(x[0]) ______ >>> type(x[0:1]) ______ >>> x[0:1] ______ >>> type(y) ______ >>> type(y[0]) ______ >>> type(y[2:3]) ______ >>> type(z[0]) ______ >>> type(z[0:1]) ______ >>> len(z[0:1]) ______ >>> len(z[0:0]) ______ >>>
As a reminder:
fd(t,length) and bk(t,length) for moving (and drawing if the pen is down) rt(t,angle) and lt(t,angle) for turningpu(t), and pd(t) for picking up and putting down the pen(x,y) points with functions like
pygame.draw.circle(screen,color,(x,y),radius,thickness) pygame.draw.lines(screen,color,closed,pointList,thickness) Some questions:
Suppose we want to make a function drawBoxes() to draw the following figure in PyGame.
| Figure 1 | Figure 2 |
|---|---|
![]() |
![]() |
Below, Figure 3 shows a function definition for drawBoxes(), and a Figure 4 shows the strategy for drawing the box (i.e. the order in which the points will be drawn).
| Figure 3 | Figure 4 |
|---|---|
def drawBoxes(screen, color, x, y, width, height): pygame.draw.lines(screen, color, False, makeBoxesPoints(x,y,width,height), 1) |
![]() |
Given that, finish the incomplete function definitions shown below. Your answer should pass the test cases given.
You only need to finish the function definition for makeBoxesPoints()—don't worry about adding any more test cases, or any of the import pygame or pygame.init() type stuff that might be needed. Don't worry about the while(True): loop or the pygame.display.update(), or any of that stuff—assume that has already been done correctly. Just focus on this one function definition.
def makeBoxesPoints(x,y,width,height): points = [] points.append( (x+width/2.0, y - height) ) # point 1 from diagram
# Fill in the rest of this function...
check_expect("makeBoxesPoints 1",
makeBoxesPoints(50,100,100,50),
[(100,50),(100,100),(150,100),(150,50),(50,50),
(50,100),(100,100)])
check_expect("makeBoxesPoints 2",
makeBoxesPoints(50,250,200,100),
[(150,150),(150,250),(250,250),(250,150),(50,150),
(50,250),(150,250)])
# @@@(end of study guide for Midterm 2)