Quiz 3 Study Guide
You are responsible for all material needed for Lab 5.
Study guides are not graded. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, or with meetings with faculty and staff. We do not provide full solutions to the study guides.
The study guide is not indicative of length, difficulty, or format of the quiz.
In addition to all concepts from Quiz 2, you should understand the following:
-
functions and stack diagrams for functions that take basic types as parameters(int, float, bool, string).
-
list data type
-
list operations and manipulation (len, indexing, append, etc)
-
accumulating lists using
append -
passing lists to functions, and stack diagrams for functions that take lists as parameters
-
functions that return values (basic types or lists) and functions with side effects (e.g., those that print or that modify a list that is passed as an argument)
Sample practice problems
-
Write a function called
get_number(lo, hi)that takes two integer arguments,loandhi, and returns an integer value betweenloandhi(inclusive). The function should repeatedly ask the user for a number until the user enters a number in the correct range. Write amain()function uses yourget_numberfunction to get a number between 1 and 10 from the user. The program should then print the number the user entered. A sample run of the program is shown below with user input in bold:Enter a number between 1 and 10: -1 This number is out of range. Please try again Enter a number between 1 and 10: 22 This number is out of range. Please try again Enter a number between 1 and 10: 7 You picked: 7 -
What would you need to change in your program above to get a number between 2 and 13 instead?
-
Analyze the following code:
m = ["t","i","m","e"] s = "time" m[1] = "a" print(m) s[1] = "a" print(s)What happens when this code is executed? What, if any, output is shown?
-
Consider the following program:
def main(): print("in main") n = 3 m = 5 z = mystery(n, m) print("n = %d" % n) print("m = %d" % m) print("z = %d" % z) def mystery(a, b): print("in mystery") result = b if a < b: result = a + b b = b - 1 # draw stack here print("a = %d" % a) print("b = %d" % b) return result main()-
Draw the function stack as it would look before the call to
mystery()returns. -
Show the output of the program when it is run.
-
-
Consider the following program:
def main(): values = [5,3,4,8] print(values) result = enigma(values) print("result: %d" % result) print(values) def enigma(ls): total = 0 val = len(ls) for i in range(len(ls)): if (ls[i] > val): ls[i] = ls[i]*2 total = total + ls[i] # Q1: draw the stack at this point in the exection return total main()-
Draw the function stack as it would look right before
enigmareturns. -
What variables are in scope at this point in the code?
-
What is the program’s output (i.e. what does it print when the program is run)?
-
What does the
enigma()function do?
-
-
The following
get_numbers(n)function doesn’t work correctly. 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 -
Consider the following assignment statements in a Python program:
letters = "abacab" words = ["we hope", "you have", "a nice", "Spring Break"] vals = [9, 11, 100, 20, 6] text = words[0]For each of the following, identify the type and value of the expression:
-
len(letters) -
len(words) -
len(vals) -
len(words[2]) -
letters[1] -
words[1] -
vals[1] -
vals[4] > len(letters) -
letters > words[1] -
text[1] -
vals[1] // 2 -
vals[1] / 2 -
vals[1] % 2 -
vals -
words
-