Lab 4 Predicates and Conditionals
Solutions
Worksheet solns
Goals
- Review conditionals
- Practice writing predicates
- “Divide, Conquer, and Glue” (DCG): functions with conditionals
Lab Setup
- Find your partner
- Use CyberDuck to SFTP into your account on the
cs.wellesley.edu
server - On the server, navigate to the
cs111/download
folder - Download the
lab04
folder to your Desktop (drag and drop it from the CyberDuck window) -
Open Thonny and view the relevant
lab04
files - Here is the worksheet as a google doc
- Open the document, then File -> Make a Copy.
- Then you can edit your copy with your answers.
Table of Contents
- Lab 4 Home
- Part 0: Warm up worksheet
- Part 1: Predicates and Functions
- Part 2: Conditionals
- Part 3: Buzzfeed-style quiz
- Reference: Using Conditionals to Disable testing code
- Knowledge Check
Big Questions
- What is a predicate, and how can you build a complex predicate out of
multiple simpler predicates?
Show Answer
A predicate is a function that always returns a boolean value. To build a complex predicate from simple ones, the complex predicate function should call the simple functions and combine their result values using boolean operators into a single boolean that it can return. - What is the difference between writing a series of
if
statements and a singleif
statement with multipleelif
parts?Show Answer
A series of if statements operate independently, and it's possible for more than one to trigger. A chain of if/elif statements guarantees that only the first statement whose condition is true will trigger, and others will be skipped even if their conditions are also true.