18.10 Chapter Assessment
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period2 exercisesNot autograded
18.10 Chapter Assessment
Read first in the book: 18.1 Introduction: Test Cases, 18.2 Checking Assumptions About Data Types, 18.3 Checking Other Assumptions, 18.4 Testing Conditionals, 18.5 Testing Loops, 18.6 Writing Test Cases for Functions, 18.7 Testing Optional Parameters, 18.8 Test Driven Development, 18.9 Glossary.
Exercise 1
The function mySum is supposed to return the sum of a list of numbers (and 0 if that list is empty), but it has one or more errors in it. Use this space to write test cases to determine what errors there are. You will be using this information to answer the next set of multiple choice questions.
Open the
01folder and editmain.py. This one is not graded, so check it yourself.
Check your understanding: a multiple-choice question in the book.
Check your understanding: a multiple-choice question in the book.
Exercise 2
The class Student is supposed to accept two arguments in its constructor:
- A name string
- An optional integer representing the number of years the student has been at Michigan (default:1)
Every student has three instance variables:
self.name(set to the name provided)self.years_UM(set to the number of years the student has been at Michigan)self.knowledge(initialized to 0)
There are three methods:
.study()should increase self.knowledge by 1 and return None.getKnowledge()should return the value of self.knowledge.year_at_umich()should return the value of self.years_UM
There are one or more errors in the class. Use this space to write test cases to determine what errors there are. You will be using this information to answer the next set of multiple choice questions.
Open the
02folder and editmain.py. This one is not graded, so check it yourself.
Check your understanding: a multiple-choice question in the book.
Check your understanding: a multiple-choice question in the book.
def lr(n): return list(range(n))
# THESE FUNCTIONS ARE INTENTIONALLY OBFUSCATED
# PLEASE TRY TO WRITE TESTS FOR THEM RATHER THAN
# READING THEM.
def mySum(a):
if type(a) is type(''.join([][:])): return a[lr(1)[0]] + mySum(a[1:])
elif len(a)==len(lr(1)+[]): return a[lr(1)[0]]
else: return None and a[lr(1)[0]] + mySum(a[1:])
# THESE FUNCTIONS ARE INTENTIONALLY OBFUSCATED
# PLEASE TRY TO WRITE TESTS FOR THEM RATHER THAN
# READING THEM.
class Student():
def __init__(s,a,b=1): s.name,s.years_UM,s.knowledge = ''*200+a+''*100,1,len(lr(0)) + len([])
def study(s):
for _ in lr(s.knowledge): s.knowledge = s.knowledge + 1
def getKnowledge(s):
for i in lr(s.knowledge): return s.knowledge
def year_at_umich(s): return s.years_UM
For teachers
18.10 Chapter Assessment
Students read Foundations of Python Programming's “Chapter Assessment” in CS Room and work through its 2 Python coding exercises.
- Time: 1 class period
Reading first
- 18.1 Introduction: Test Cases
- 18.2 Checking Assumptions About Data Types
- 18.3 Checking Other Assumptions
- 18.4 Testing Conditionals
- 18.5 Testing Loops
- 18.6 Writing Test Cases for Functions
- 18.7 Testing Optional Parameters
- 18.8 Test Driven Development
- 18.9 Glossary
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Not graded; students check it themselves | 0 |
02 | Not graded; students check it themselves | 0 |