Quiz 4 Study Guide

Quiz Study Guides are provided as a courtesy. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, on EdSTEM, or during meetings with faculty and staff. We do not provide full solutions to the quiz guides.

You are responsible for all material covered through the end of Week 8.

In addition to all concepts from Quiz 3, you should understand the following:

Python concepts

  • object oriented programming

  • graphics objects

  • instance of a graphics object

  • data and methods of an object

  • animation

  • mutability of objects

  • lists and strings as objects

  • list methods (e.g., append(), len(), repetition, concatenation)

  • top-down design

  • stubbed-out (prototyped) function

  • list of lists

Graphics Objects and Methods

  • GraphWin: setBackground(), getMouse(), setCoords()

  • Point: getX(), getY()

  • Circle: getCenter(), getRadius()

  • Line: getP1(), getP2()

  • common methods for graphics objects: draw(window), move(dx, dy), setFill(color)

  • also color_rgb(r,g,b)

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()
  2. 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.

  3. 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()
  4. The following get_numbers(n) function doesn’t work. Find and fix the bug in the code below.

    def get_numbers(n):
        """
        Purpose: Read n numbers from the user and return them as a list
        Parameters: n -- the number of values to read from the user (integer)
        Return: a list of the numbers entered by the user
        """
        for i in range(n):
        	value_list = []
    	value = int(input("Enter a number: "))
    	value_list.append(value)
        return value_list
  5. 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.

  6. Assume you have these two functions already written:

    • getPick() asks the user for "r,p,s?" and returns:

      • "rock" if they enter r

      • "paper" if they enter p

      • "scissors" if they enter s

    • winner(user,comp) returns:

      • "user" if user won the game (e.g., user="rock", comp="scissors")

      • "comp" if comp won the game (e.g., user="rock", comp="paper")

      • "tie" if it’s a tie game (e.g., user="rock", comp="rock")

        Write a main() function that uses the above functions to play one round of rock-paper-scissors. Here are a few sample runs of the program:

        $ python3 rps.py
        r,p,s?: r
        Computer chose rock
        tie...
        $ python3 rps.py
        r,p,s?: r
        Computer chose paper
        Computer wins!
        $ python3 rps.py
        r,p,s?: r
        Computer chose scissors
        You win!
  7. Write the stub for the winner(user, comp) function.

  8. Write the implementation for the getPick() function. Your getPick() function should only accept r, p, or s from the user. If they enter anything else, print an error message and ask again.

    r,p,s?: w
    please enter r, p, or s!!!
    r,p,s?: zebra
    please enter r, p, or s!!!
    r,p,s?: r
  9. 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()