CS21: Quiz 3 Study Guide

In addition to the concepts from Quiz 1 and Quiz 2,

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. What would the output of the following be?
    for i in range(5):
      for j in range (3):
        print "%d %d %s" % (i,j,(i+j)*"X")
    
  2. Write a program that asks the user for a positive integer, n, and then calculates and displays the sum of all numbers from 1 to n. For example, if the user enters 5, the program calculates 1 + 2 + 3 + 4 + 5

  3. 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...
    
    grade  1: 98
    grade  2: 87
    grade  3: 65
    grade  4: 95
    grade  5: 80
    grade  6: -1
    
    The average of those 5 grades is 85.000
    
  4. 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.
    
    
  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. What would be the TYPE of each of these?
       p = Point(100,200)             type of p?
       c = Circle(p, 5)               type of c?
       x = c.getRadius()              type of x?
       c2 = c.clone()                 type of c2?
       L = [c, c2]                    type of L?
       m = L[0]                       type of m?
       y = L[1].getCenter().getY()    type of y?
    
    
  7. Given the assignments for S and L, what is the value and type of each expression?
    L = ['Join me', 'and we can rule', 'the galaxy', 'as father and son']
    S = "abcdefg"
    
    len(L)
    
    len(S)
    
    range(len(S))
    
    "a" in L[2]
    
    "ABC" in S
    
    S.upper()
    
    L[0][0]