CS8, 09M, UCSB
H05: (Based on Miller/Ranum, Chapter 3 (pp. 91-92) and Chapter 4 (up to p. 128) Total points: ? (printable PDF)
Accepted: on paper, in lab (either at 11am, or 12:30pm, in Cooper Lab) on Thursday, August 20th
No email submission allowed.
Name: (2 pts)______________________________ UCSBNetID (2 pts) _____________________
Lab Section (1 pts) Circle one: Thu 11am Thu 12:30pm Unknown
Circle the one you plan to attend, which may or may not be the one you are registered for.
Review pp. 91-92 of Chapter 3. Then answer these questions:
Please turn over for more problems
Continued from other side
>>> for name in ["Dave", "Sheila", "Maria"]:
print("Hello " + name)
Hello Dave
Hello Sheila
Hello Maria
>>>
Given that, what would be the output of the following?>>> total = 0
>>> for name in ["Dave", "Sheila", "Maria"]: total = total + len(name) >>> print("total = " + str(total))
def dogCount(theList):
count = 0 # initialize count
for word in theList:
if word=="dog":
count = count + 1
return count
check_expect("dogCount test 1", dogCount(["dog","cat","dog"]), 2)
check_expect("dogCount test 2", dogCount(["cat", "mouse", "tiger"]), 0)
check_expect("dogCount test 3", dogCount(["cat", "dog", "tiger"]), 1)
check_expect("dogCount test 4", dogCount([]), 0) # [] is the empty list
We know from the reading in Chapter 4 that we can have a list of numbers too, such as [3,5,-9,2,4,-8] or [2,3,87,1,-2]. >>> countNeg([4,20,-32,14,-56])You should get the answer 2 back. Try it on other lists as well.