## 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:

### Python concepts

• lists and strings as objects

• list methods (e.g., `append`)

• string methods (e.g., `split`, `join`, `strip`, `isdigit`, `isalpha`)

• top-down design

• stubbed-out (prototyped) function

• file I/O (e.g., `open`, `readlines`)

### Practice problems

1. Write a function called `isVowel(letter)` that has one parameter, `letter`. This function should return `True` if the letter is a vowel (upper or lowercase), `False` if not. For example, calling `isVowel("i")` should return `True`, and `isVowel("Q")` should return `False`.

2. 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 `return count` statement.

``````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()``````
3. The following `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``````
4. 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 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!```
5. Write the stub for the `winner(user, comp)` function.

6. Write the implementation for the `getPick()` function. Your `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```
7. Given the assignments for `S`, `L`, and `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[2]

"cs" in S

L[1]

len(L[1])

s.split()

N``````
8. 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 `fix_list` and `find_max`:

``````def main():

infile = open("numbers.txt", "r")

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()``````