Quiz 4 Study Guide
Quiz Study Guides are provided as a courtesy. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, on EdSTEM, or during meetings with faculty and staff. We do not provide full solutions to the quiz guides.
You are responsible for all material covered through the end of Week 8.
In addition to all concepts from Quiz 3, you should understand the following:
lists and strings as objects
list methods (e.g.,
string methods (e.g.,
stubbed-out (prototyped) function
file I/O (e.g.,
Write a function called
isVowel(letter)that has one parameter,
letter. This function should return
Trueif the letter is a vowel (upper or lowercase),
Falseif not. For example, calling
For the following program, show the full output when the program is run to completion, and draw the stack as it would look just before the computer executes the
def main(): words = ['roses','are','red'] print(words) line = 'violets are blue' result = update(words,line) print(result) print(words) ################ def update(L, S): count = 0 data = S.split() for item in data: if item not in L: L.append(item) count = count + 1 # draw stack here return count main()
get_numbers(n)function doesn’t work. 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
Assume you have these two functions already written:
getPick()asks the user for "r,p,s?" and returns:
"rock" if they enter r
"paper" if they enter p
"scissors" if they enter s
"user" if user won the game (e.g., user="rock", comp="scissors")
"comp" if comp won the game (e.g., user="rock", comp="paper")
"tie" if it’s a tie game (e.g., user="rock", comp="rock")
Write a main() function that uses the above functions to play one round of rock-paper-scissors. Here are a few sample runs of the program:
$ python3 rps.py r,p,s?: r Computer chose rock tie... $ python3 rps.py r,p,s?: r Computer chose paper Computer wins! $ python3 rps.py r,p,s?: r Computer chose scissors You win!
Write the stub for the
Write the implementation for the
getPick()function should only accept r, p, or s from the user. If they enter anything else, print an error message and ask again.
r,p,s?: w please enter r, p, or s!!! r,p,s?: zebra please enter r, p, or s!!! r,p,s?: r
Given the assignments for
N, what is the value and type of each expression?
S = "CS21 Rocks" L = ["May", "the Force", "be", "with you."] N = "".join(L) VALUE TYPE L len(L) len(S) "a" in L "cs" in S L len(L) s.split() N
Given an input file named "numbers.txt" that consits of some number of lines, each with a single float value. For example, the first few lines of the file might look like:
4.5 -0.5 32.1 -8 54.1 12.3
Complete the program below to implement the functions
def main(): infile = open("numbers.txt", "r") num_list = infile.readlines() fix_list(num_list) max = find_max(num_list) print("Max value in list of %d values is %f" % (len(num_list), max)) ######################## # implement the fix_list function # implement the find_max function ######################## main()