Week 11
Getting kind of loopy (Loops, part 1)
Weekly Summary
Recall from our prior Scratch Unit that we defined loops as:
For the next two weeks we will be looking at writing loops in Python. It takes us two weeks not because loops themselves are difficult - although there are two very distinct ways to write loops in Python - but because there are so many INTERESTING problems that involve loops.
This week we will start by looking at the two types of loops and using them with some "simple" numerical problems.
Learning Outcomes
By the end of the week students should be able to:
- Reading Code
- Given a pre-defined range() function indicate what values are in the list produced by the function.
- Given a snippet of code containing a for loop, indicate how many times that loop will execute.
- Given a snippet of code containing a while loop, indicate how many times that loop will execute.
- 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.
- Functions will include those requiring looped behavior using for and/or while loops
- 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
POGIL Materials from the CoP
Prior to starting this week's materials you should make sure that you have done some activities that were completed at the Community of Practice event on Saturday, November 2nd. These include:
- Complete the POGIL models 1 and 2
- If helpful, watch the debrief videos that I made for your facilitators to help them facilitate the discussions about these Models:
- Model 1 - The range() function
- Model 2 - Comparing for loops and while loops
Textbook Readings
- Log on to your online textbook
- Read the following sections from your textbook
- 6.1. Updating variables
- 6.2. The while statement
- 6.3. Infinite loops
- 6.4. Finishing iterations with continue - You may skip this section. I neither teach, nor encourage you to use, the continue command in this course.
- 6.5. Definite loops using for
- 6.6. Loop patterns
- 6.7. Debugging
Lesson Videos
NOTE: For the rest of the semester I'm going to set up many of my lesson videos to give you the OPTION of trying to write the code before me and then use my videos as guidance, an alternative solution, or just a plain old lecture. I want to give YOU the opportunity to use these the way that YOU most need them.
In many of the following activities I will start by giving you the description of a program that you COULD try to write. If you do this, I strongly encourage you to give this an honest effort. Most of you will succeed. When you are done, or if you hit a stumbling block and can't finish it, you can/should watch the followup video where I show you how I would do this. This is kind of a step between the lesson videos and the paired programming activity. I want you to think about the problem. But I also will use these as a way to teach you something "new."
- An Introduction to Loops - with special guests, Sheldon and Penny from "The Big Bang Theory"
- Using a for loop to solve mathematical problems
- Using Sigma notation to add up a series of numbers
- Determining how many times we get heads if we flip a coin N times
- Building a Loan Ammortization Table
- Using while loops
- Flipping a coin until you get N heads
- Checking if an input is valid
- This one doesn't have an introduction. I just show you an idea.
- My solution video
- Create every dad's favorite car ride game - "I'm thinking of a number"
- You can always use a while loop - but the reverse isn't true.
- ONLY if you are interested... solving the first three problems using while loops instead of for loops:
Practice Materials
Paired Programming Activity
Please complete the following Paired Programming activity
Additional Textbook Activities
These are great for some additional practice. You should complete these on your own rather than with a partner.
Code Walkthroughs
The following are videos and code solutions for the programs discussed in the Paired Programming Activity . You SHOULD NOT view these until you have either solved the activities yourself - and you want to see how I would do it - or you have tried at least two different times and still are stumped. Don't jump to reading the "correct answer" too quickly. I think it is a GOOD thing to struggle with a problem for a day or two. We improve when we meet and overcome resistance. Learning comes from practice. You need to TRY before jumping to the solutions.
- LOOP 0 - discountTable()
- LOOP 1 - collatz()
- LOOP 2 - howLong()
- LOOP 3 - finalPopulation()
Graded Materials
The following programs are formally graded as part of the Unit 3 "Problem Set." You should work on these only as an individual. While working on these problems you may refer to your notes, your textbook, any programs you wrote, and even my videos. However, you should limit your discussion of these programs with classmates. It is acceptable to discuss how you solved a problem in grand scheme of things, but you should never show your code to a classmate either as the person who is struggling or as the person who is helping a struggling classmate. You can talk ideas, but not specific solutions.
- Graded Loop problem set (Part 3.1 of Problem Set #3).