Quiz 3 Study Guide
You are responsible for all material needed for Labs 1-5 which
includes for loops, if statements, boolean operators, relational
operators, while loops, and functions.
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, on Slack, or with meetings with faculty and staff. We do not provide full solutions to the study guides.
In addition to all concepts from Quiz 2, you should understand the following:
Python concepts
- 
while loops with flag variables 
- 
stack diagrams 
- 
scope of a variable 
- 
function definition 
- 
function call 
- 
function parameters 
- 
function arguments 
- 
functions with returns 
- 
functions done for side effects 
- 
mutable and immutable types 
- 
list manipulation (accumulators, indexing, etc) 
Practice problems
- 
Write a program which asks the user for 4 words and then prints them in reverse order. Hint: store them in a list. Here’s an example run of the program: word 1: hello word 2: zebra word 3: unicorn word 4: corgi corgi unicorn zebra hello 
- 
Write a function called avgListthat, given a list of numbers, calculates and returns their average. For example,avgList([5, 10, 5, 4])should return 6. Write amainfunction to test the function in a complete program.
- 
Consider the following program: 1 def mystery(word, letter): 2 print("word: %s" % (word)) 3 print("letter: %s" % (letter)) 4 result = "" 5 for ch in word: 6 if ch != letter: 7 result += ch 8 9 # draw stack here 10 return result 11 12 def main(): 13 test = "hello" 14 check = "l" 15 answer = mystery(test, check) 16 print("the answer is %s" % (answer)) 17 18 main()- 
Draw the function stack as it would appear when you reach line 9. (In gradescope you will need to upload your picture. Be sure you can do this by taking the practice quiz in gradescope.) [answer] 
- 
What variables are in scope at line 9? 
- 
What is the program’s output (i.e. what does it print when the program is run)? 
- 
What does the mysteryfunction do?
- 
What would mystery("moobaalalala", "a")return?
- 
What would mystery("zoo", "a")return?
 
- 
- 
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 
- 
Analyze the following code: m = ["t","i","m","e"] s = "time" m[1] = "a" s[1] = "a"What happens when this code is executed? What are the values of mandsafterward?
- 
Consider the following program: def is_odd(number): # Q1: draw stack here (the first # time the function is called) if number % 2 == 0: return False else: return True def enigma(ls): total = 0 for i in range(len(ls)): if is_odd(ls[i]): ls[i] = ls[i]*2 total = total + ls[i] return total def main(): values = [5,3,4,8] print(values) result = enigma(values) print("result: %d" % result) print(values) main()- 
Draw the function stack as it would look the first time is_odd()is called. [answer]
- 
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?
 
- 
- 
Write a function called getIntthat has two parameters,loandhi. The function should prompt the user to enter an integer betweenloandhi(inclusive), and then return the value entered. Your function should not return until the user enters a valid integer in the rangelotohi. You may assume bothloandhiare positive integers.Now add a main()function that callsgetIntto get an integer from 1-10. Yourmain()should then print out "Boo!" that many times.Here’s an example run of the full program: Please enter an integer from 1-10: 99 Please enter an integer from 1-10: -40 Please enter an integer from 1-10: 4 Boo! Boo! Boo! Boo!