Introduction to Computer Science

Quiz 3 Study Guide

This page contains a list of things you should know before taking Quiz 3.

List Operations

We’ve discussed lists in greater detail recently. You should know the basic list operations we’ve used in class and how to modify lists.

Exercises

Write Python code which will perform each of the following operations on a list named alist.

for and while loops

We discussed the while loop, which is similar to for (in that it is a loop) but behaves differently (in that it runs until a condition holds rather than running through a list of values). You should understand when it is appropriate to use each kind of loop. You should be able to follow the execution of a while loop and identify when it terminates.

Exercises

Predict (without the use of a computer) what each of the following programs will print.

Functions

We introduced functions and described how they can be used to group and reuse lines of code. You should understand how a program executes when a function is called. You should understand how arguments are assigned to parameters when a function is called and how values are returned from functions. You should also understand how lists and other objects behave with respect to functions.

Exercises

Predict (without the use of a computer) what each of the following programs will print.

Debugging

As with the last quiz, you should know how to find mistakes in small programs. Given a description of what a program should do and the program itself, you should be able to identify how it can be fixed. (Hint: there may be more than one thing wrong with a program.)

Exercises

For each of the following descriptions, a program is provided which is an incorrect implementation. Identify how that implementation can be corrected to conform with the description.

#1

Description

This program was intended to create ten circles randomly positioned on a window.

Program

from graphics import *
from random import *
def main():
  win = GraphWin("Window",800,800)
  x = randrange(800)
  y = randrange(800)
  for n in range(10):
    circle = Circle(Point(x,y),10)
    circle.draw(win)
  win.getMouse()
main()

#2

Description

This program was intended to count the number of vowels in a word.

Program

def count_vowels(word):
  word = "Mississippi"
  vowels = 0
  for c in word:
    if c in "aeiouAEIOU":
      vowels += 1
  return vowels
def main():
  word = raw_input("What's the word? ")
  print "There are %d vowels in that word." % count_vowels(word)
main()

#3

Description

This program was intended to start from 2 and keep printing and squaring the number until it exceeds one billion.

Program

def main():
  number = 2
  while number > 1000000000:
    print number
    number = number * number
main()

#4

Description

This program creates a window and draws a circle in each place the user clicks.

Program

from graphics import *
from random import *

def draw_circle_in_window(click):
  win = GraphWin("Window",800,800)
  circle = Circle(click, 10)
  circle.setFill("red")
  circle.draw(win)
  
def main():
  win = GraphWin("Window",800,800)
  while True:
    click = win.getMouse()
    draw_circle_in_window(click)

main()