In addition to the concepts from Quiz 1
and Quiz 2,
You should understand and/or be able to use the following Python concepts:
- definite (for) loop
- indefinite (while) loop
- nested loops
- lists and strings as objects
- list and string methods (e.g., strip(), split(), upper(), append(), index())
- python in (and not in) operator used on lists and strings
- random library functions: randrange(start, stop), choice(list), etc.
- formal parameter
- actual parameter (also known as an argument)
- function definition
- function call
- call stack
- scope of a variable
I suggest you try doing these problems on a sheet of paper and not on
the computer. This will give you practice under a more quiz-like
sitituation. After you have written your solution down on paper, then
try enter the program on the computer to see if you were right.
- What would be the output of the following?
for i in range(5):
for j in range (3):
print "%d %d %s" % (i,j,(i+j)*"X")
- 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
- 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
- Given the assignments for S and L, what is the value and type of
L = ["There are", "many", "like it,", "but this one", "is mine."]
S = "abcdefg"
"a" in L
"ABC" in S
- Write a function called getLetter() that asks the user for a letter
and returns the given letter. Your function should check to make sure the user
enters just a single letter. If the user enters anything other than a single
letter, the function should ask again, until it receives valid input.
- Write a function called avgList that takes a list of
numbers as input and returns their average. For example,
avgList([5, 10, 5, 4]) should return 6. Write a
main program to test the function.
- Write a function called maxList that takes a list of
numbers as input and returns the largest number in the list. For
example, maxList([7,4,8,1]) should return 8. Write
a main program to test the function.
- Consider the following program.
print "in main"
test = "whee"
check = "e"
answer = helper(test, check)
print "the answer is", answer
def helper(word, letter):
print "in helper"
print "word:", word
print "letter:", letter
x = 0
for ch in word:
if ch == letter:
x = x+1
# draw stack here
- Draw the call stack as it would look just prior to returning from
- What is the program's output (i.e. what is printed when the program is run)?
- What does the helper function compute?
- What would helper("moobaalalala", "a") return?
- What would helper("zoo", "a") return?
- Trace through the following code, show the output from its entire
execution, and draw the stack as it would look before the return statement in
def main(): OUTPUT STACK
print "in main"
b = foo(5)
print "in foo"
biglist = 
for i in range (n):
sublist = ["h", i, i+10]
# draw stack here
What is the type and value of each of the following expressions: