Quiz 4 Study Guide
Special Note: since we have now switched to online learning, this quiz will be a little different from the previous paper and pencil quizzes. For this quiz you will be given some programs to write or fix. They will be in a certain location in your cs21 directory, on the CS computers. You will be able to use atom to write the programs, and to ssh to the CS computers to run them and test them. Once you have written each program, you will run handin21 to turn them in. You will still have a time limit for writing the programs (probably 30 minutes), but you will be able to "take the quiz" anytime during the week (whenever you have time and the network is working!). So practice for this quiz by writing the functions and programs below. More details coming soon, but let us know if you have any questions so far.
You are responsible for all material covered through the end of Week 8 (TDD).
In addition to all concepts from Quiz 3, you should understand the following:
Python concepts
-
using objects, data and methods of an object
-
mutability of objects
-
lists and strings as objects
-
common string methods (e.g.,
upper()
,lower()
,split()
,isdigit()
,isalpha()
) -
adding to a list with the
append()
method -
top-down design
-
bottom-up implementation
-
stubbed-out (prototyped) functions
-
list of lists
Practice problems
-
The following
isVowel(letter)
function doesn’t work. For example, callingisVowel("a")
returnsTrue
, but so doesisVowel("b")
. Find and fix the bug inisVowel(letter)
def isVowel(letter): """ letter: a single character (string) returns: True if given letter is a vowel, False if not """ letter = letter.lower() if letter=="a" or "e" or "i" or "o" or "u": return True else: return False
-
Given the assignments for
S
andL
, what is the value and type of each expression?S = "abcdefg" L = ['Join me', 'and we can rule', 'the galaxy', 'as father and son'] VALUE TYPE ----- ---- len(L) len(L[0]) len(S) "a" in L[2] "ABC" in S L[0][0] S.upper() S.isdigit() L[1].split() S < "zebra"
-
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
-
-
winner(user,comp)
returns:-
"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 I chose rock tie... $ python3 rps.py r,p,s: r I chose paper I WIN! $ python3 rps.py r,p,s: r I chose scissors you win :(
-
-
-
Now write and add the
getPick()
function to your rock-paper-scissors program. YourgetPick()
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
-
Now write and add the
winner(user,comp)
function. Hint: check for the "tie" case first, then all cases where the user wins, leaving the "comp" case for last.