CS21: Quiz 3 Study Guide

In addition to all concepts from Quiz 1 and Quiz 2...

You should understand and/or be able to use the following Python concepts:

• definite for loop
• indefinite while loop
• nested loops
• basic functions
• lists and strings as objects
• some list and string methods (e.g., append(), upper(), lower())
• python in (and not in) operator used on lists and strings
• random library functions: randrange(start, stop), choice(<sequence>)
• object oriented programming
• graphics objects
• instance of a graphics object
• data and methods of an object
• pixels
• animation

You should understand how to make instances of the following graphics

objects and how to use their methods:

• GraphWin: setBackground(), getMouse()
• Point: getX(), getY()
• Circle: getCenter(), getRadius()
• Line: getP1(), getP2()
• common methods for graphics objects: draw(window), move(dx, dy), setFill(color), clone()
• also color_rgb(r,g,b)

Practice problems:

1. What would the output of the following be?
for i in range(2):
for j in range (4):
print("%d %d %s" % (i,j,(i+j)*"X"))
1. For the following program, briefly describe what it does and draw a picture it might produce.
from graphics import *
from random import choice

alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
colors = ["red","green","blue"]

gw = GraphWin("QUIZ",200,200)
p1 = Point(0,0)
p2 = Point(50,50)
first = Rectangle(p1,p2)
first.setFill("black")
first.setOutline("white")

for i in range(4):
for j in range(4):
block = first.clone()
block.move(i*50,j*50)
block.draw(gw)
RL = choice(alph)
letter = Text(block.getCenter(), RL)
letter.setTextColor(choice(colors))
letter.draw(gw)
gw.getMouse()
1. Write a function called isVowel(letter) that has one parameter, letter. This function should return True if the letter is a vowel (upper or lowercase), False if not. For example, calling isVowel("i") should return True, and isVowel("Q") should return False.

2. Write a program that reads in student grades from the user, until the user enters a -1. After reading in the -1, the program should compute and display the average grade.

Please enter your grades below.
Enter a -1 when you are all done...

The average of those 5 grades is 85.000

3. Write a function called results(np, nc) that takes two parameters, the number of problems, and the number correct. The function should print an appropriate message to the user, based on the percentage the user got correct. For example, if they got 100% correct, print "Super!"; if they got 80% correct, print "Good job."; and so on. Here's a few examples:

• calling results(10,10): You got 10 out of 10 correct. Super!
• calling results(5,1): You got 1 out of 5 correct. Try again...
• calling results(8,6): You got 6 out of 8 correct. Not bad.
1. Write a program that asks the user for a number (positive integer, n), and then asks the user to click the mouse n times, anywhere in a graphics window, each time drawing a small red circle where the user clicked.

2. Given the assignments for S, L, P, and C, what is the value and type of each expression?

s = "abcdefg"
L = ['Join me', 'and we can rule', 'the galaxy', 'as father and son']
P = Point(100,200)
C = Circle(P, 5)
                             VALUE            TYPE
-----            ----
len(L)

len(S)

range(len(S))

"a" in L[2]

"ABC" in S

S.upper()

L[0][0]

P.getX()

P.getX() > 600

C.getCenter().getY()