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
.
- Print the number of elements it contains.
- Add an element to the end of the list.
- Change the first element of the list to
0
. - Add up all of the elements of the list. (For this, assume all of the list’s elements are numbers.)
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.
-
n = 0 while n < 10: print n n += 1
-
number = 3 while number > 1: if number % 2 == 1: number = number * 3 + 1 else: number = number / 2 print number
-
i = 0 b = [True, True, True, False, True] while b[i]: i += 1 print b[i]
-
i = 1 while i % 5 != 0: while i % 3 != 0: i += 1 print i i += 1 print "Done!"
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.
-
def one_up(n): n = n + 1 print n def main(): x = 2 one_up(1) one_up(x) one_up(x) main()
-
def longstring(s,n): s = s + str(n) return s * n def main(): s = longstring("a", 2) print s print longstring(s, 3) main()
-
def has_a_problem(string_list): for string in string_list: if string == "problem": return True return False def main(): x = ["Want","a","program","to","do","things?"] y = ["No","problem","at","all"] if has_a_problem(x): print x if has_a_problem(y): print y main()
-
def trick_question(): print "The trick" def main(): print "Not a trick" main()
-
def more_cowbell(x): x.append("cowbell") def main(): y = ["music"] more_cowbell(y) print y more_cowbell(y) print y main()
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()