Week 10
More Functions
Weekly Summary
Last week we introduced functions
All of the code we wrote last week was taking code we had already written as basic IPO problems and converting it to functions. This week we will practice more with functions but we will work with entirely new examples.
Learning Outcomes
By the end of the week students should be able to:
- Reading Code
- Given an existing function, and a set of input parameters to that function, identify the output returned by the function.
- Writing Code
- Given a problem statement that includes the name of a function and the parameters required by that function, write the Python code/definition of the provided function such that it solves the problem by returning correct output values.
- For now, functions will be limited to those using
- Mathematical Operators
- Boolean Operators
- Conditional Expressions
- For now, functions will be limited to those using
- Given a problem statement that includes the name of a function and the parameters required by that function, write the Python code/definition of the provided function such that it solves the problem by returning correct output values.
- Debugging Code
- Given a problem statement and a non-working Python function that attempts to solve the problem, identify where the function is incorrect and explain how to fix it.
Learning Materials
Our Lessons - Writing some new functions
- VIDEO - Is a right triangle?, version 1
- VIDEO - Is a right triangle?, version 2
- VIDEO - Is a right triangle?, version 3
- There is a mistake at about 6:25 in this video where I had intended to use isRightTriangleV2() and show that it still worked for the False values, but I inadvertently typed in isRightTriangle() instead [Version 1].
- This is a mistake, but the same answers would have come back with V2
- [Thanks to one of your peers who caught this mistake and notified me!]
- VIDEO - When is Easter?
- Here is the website that explains the "rules" for Easter
- VIDEO - What color is the square?
- Here is the website that explains the "rules" for the colors on a chess board
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.
- 5.15. Mixed-up Code Questions
- Problem 7
- Problem 8 - This one is challenging. Do what you can to figure it out!
"Solutions" for these two programs are included in the Code Walkthrough section below.
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.
- 5.14. Multiple Choice Questions
- 5.15. Mixed-up Code Questions
- Remember that you can switch these to "Write Code" mode
- 5.16. Write Code Questions
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 - 5.15.7
- Parsons Problem 2 - 5.15.8
- Debugging Problem 1 - As the crow flies
- Pair Practice 1 - windchill()
- Pair Practice 2 - fahrToCel()
- Pair Practice 3 - carFee()
- Pair Practice 4 - taxesOwed()
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 and the Rubber Duck AI from CS50. If you are getting stuck this week you should absolutely pull out your rubber duck and talk to it. If that isn't working, jump to Rubber Duck AI. Both are OK on your practice/homework (even the graded materials) although both will be off limits during your CD in a week.
- Graded Functions problem set, part 2 (part of the Unit 2 Problem Set).