Week 8
Conditional Execution
Weekly Summary
How do you write programs when you need to do different things in different situations? The answer is, you use conditionals.
Recall from our prior Scratch Unit that we defined conditionals as:
This week we will begin to consider programs that use conditional statements to modify how code behaves.
Learning Outcomes
By the end of the week students should be able to:
- Reading Code
- Given a Boolean expression using one of the six Boolean operators (<, <=, >, >=, == or !=), indicate the evaluated output (True or False).
- Given a compound Boolean expression (one that includes one or more of AND, OR, or NOT, indicate the evaluated output (True or False).
- Explain the difference in Python between = and ==.
- Given a small script (including data with values) containing an if statement, indicate the output of the script.
- Given a small script containing an if/else statement, indicate the output of the script.
- Given a small script containing an if/else statement with one or more elif statements, indicate the output of the script.
- Writing Code
- Given an IPO problem statement that includes conditionals (if, if/else and if/elif/else statements), write the python code that will solve the problem.
- Debugging Code
- Given a problem statement and a non-working program that attempts to solve the problem, identify where the code is incorrect and explain how to fix it.
Learning Materials
POGIL Activity#4
Don't worry. You didn't miss #3. I just didn't use it in the course. [These POGIL activities are part of a set created by a different CS faculty member and used under the Creative Commons license]
If we were working in a face-to-face classroom I would have you complete the following POGIL activity in groups of 3 or 4 [which is the intention of POGIL activities]. Since we are not face-to-face I give you the option of either:
- completing this face-to-face with a small group of peers (do you have classmates in the same building or school district?)
- completing this online with a small group of peers (reach out to some classmates from your CoP sessions)
- completing this on your own
The choice is yours and you don't have to tell me (because I'm not collecting this activity. It's for learning purposes, not grading purposes).
- Explanation video
- In my classroom, we would have several opportunities to talk about what you were doing in the POGIL. Online, that gets lost.
- You can watch this video AFTER you have completed POGIL #4 to help you debrief what happened.
- VIDEO: What was going on in POGIL #4 (Again, do not watch this video until you have completed POGIL #4)
Textbook Readings
- Log on to your online textbook
- Read the following sections from your textbook
- 4.1. Boolean expressions
- NOTE: This section discusses two operators I will not expect you to use/know
- is
- is not
- NOTE: This section discusses two operators I will not expect you to use/know
- 4.2. Logical operators
- 4.3. Conditional execution
- 4.4. Alternative execution
- 4.5. Chained conditionals
- 4.6. Nested conditionals
- 4.9. Debugging
- 4.1. Boolean expressions
- Depending on where you are at in your programming journey you may find the following OPTIONAL sections helpful. Having said that, this is not material that is part of my course outcomes.
Our Lessons - Working with Problems that use Conditionals
- VIDEO - How much should I tip?
- VIDEO - Paycheck that includes overtime
- VIDEO - Odd or Even??
- Having a vowel movement
- Can I join the Marines?
- VIDEO - Version 1, Nested Conditionals
- VIDEO - Version 1, Clean up the Spaces
- VIDEO - Version 2, Compound Conditional
Practice Materials
Parsons Problems
These problems present the complete code for a program, but in a mixed-up order. Your task is to arrange the pieces correctly to "write" the described program. These problems are a great first step in learning because they allow you to focus on the semantics of programming rather than the syntax.
- 4.12. Mixed-up Code Questions
- Problem 1
- Problem 2
- Problem 3
- Problem 4
"Solutions" for these four programs are included in the Code Walkthrough section below.
You are welcome to complete additional problems from this section.
Debugging Problems
Fixing programs that are "almost" correct but not yet functional is a crucial skill. As teachers, we often assume students will develop this skill by debugging their own work. However, it can be beneficial to provide explicit practice in debugging. The following problem is designed to give you semi-guided practice in debugging.
The solution for this program is included in the Code Walkthrough section below.
(Paired) Practice Programs
Each week, I will provide you with several practice programs (non-graded) that I encourage you to attempt, with your partner, before tackling the graded assignments.
Additional Textbook Activities
These are great for some additional practice. You should complete these on your own rather than with a partner.
- 4.11. Multiple Choice Questions
- 4.12. Mixed-up Code Questions
- Remember that you can change them from Parsons Problems to Active Write Code.
- 4.13. Write Code Questions (1-3 and 5-8)
Code Walkthroughs
The following are videos and code solutions explaining the problems above, including the Parsons Problems, Debugging Problems, and (Paired) Programming Activity. You SHOULD NOT view these until you have either solved the activities yourself and want to see how I would do it, or you have tried at least twice and are still stumped.
Don’t jump to the "correct answer" too quickly. It’s beneficial to struggle with a problem for a day or two. We improve when we meet and overcome resistance. Learning comes from practice, so make sure to TRY before jumping to the solutions.
- Parsons Problem 1 - 4.12.1
- Parsons Problem 2 - 4.12.2
- Parsons Problem 3 - 4.12.3
- Parsons Problem 4 - 4.12.4
- Debugging Problem 1 - Bad Odd or Even
- Pair Practice 1
- Using Nested Conditionals
- Using Chained Conditionals
- Pair Practice 2 - Is this QB having a good year?
- Pair Practice 3 - Chinese Zodiac
- Pair Practice 4 - Da Binz Store
Graded Materials
The following programs are formally graded as part of the Unit 2 “Problem Set.” You should work on these individually. While working on these problems, you may refer to your notes, textbook, any programs you wrote, and my videos. However, you should significantly restrict your discussions with classmates. It is moderately acceptable to discuss how you solved a problem in general terms, but you should never show your code to a classmate, whether you are the one struggling or the one helping. You can talk about ideas, but not specific solutions.
Don't forget about the concept of Rubber Duck Debugging. If you are getting stuck this week you should absolutely pull out your rubber duck and talk to it. YES, you will feel foolish at first. But, again, the research shows that by talking out loud you can often (not always, but often) figure things out on your own. TRY IT!
- Graded Conditionals problem set (part of the Unit 2 Problem Set).