CS21: Quiz 4 Study Guide

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

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

You should understand how to make instances of the following graphics

objects and how to use their methods:

Practice problems:

  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. For the following program, show the full output when the program is run to completion, and draw the stack as it would look just before the computer executes the return count statement.

def update(L, S):
  count = 0
  data = S.split()
  for item in data:
    if item not in L:
      L.append(item)
      count = count + 1
  # draw stack here
  return count

def main():
  words = ['roses','are','red']
  print(words)
  line = 'violets are blue'
  result = update(words,line)
  print(result)
  print(words)

main()

Question 3 Answer Key

  1. Write a program that reads in the lines from a file called infile.txt and creates an acronym from the first letter of each line. For example, if the input file is:
Thanks
Goodness
It's
Friday

Your program will create and print out the string "TGIF"

  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)

"a" in L[2]

"ABC" in S

L[0][0]

P.getX()

P.getX() > 600

C.getRadius()

C.getCenter().getY()