It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home 1 .
Note5.33.1.
If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
Learning Objectives
Students will know and be able to do the following.
Content Objectives:
Recognize a for-each loop.
Recognize a range.
Recognize a for loop.
Recognize a while loop.
Process Objectives:
Predict the outcome of code with lists and loops.
Modify code with loops.
Fix code that processes a list with loops.
Subsection5.33.1The For-Each Loop
A for-each loop in Python will loop though the items in a list starting with the item at index 0, then index 1, and so on till the last item in the list.
Checkpoint5.33.2.
Q-1: What is the first thing that will be printed by the code below?
Checkpoint5.33.3.
Q-2: What is the last thing that will be printed by the code below?
Checkpoint5.33.4.
Run this code to see what it prints.
Checkpoint5.33.5.
Q-4: What is the name of the variable that can be used to refer to the current list item in the for-each loop?
Checkpoint5.33.6.
Drag the blocks from the left and put them in the correct order on the right to define a function total_even that returns the total of the even numbers in the passed list.
def total_even(alist):
---
Def total_even(alist): #paired
---
total = 0
---
for num in alist:
---
for num in alist #paired
---
if num % 2 == 0:
---
if num % 2 == 1: #paired
---
total += num
---
return total
---
Return total #paired
Subsection5.33.2Range and For
How do you loop just a set number of times? You can use the built-in range function to do this.
Checkpoint5.33.7.
Q-6: What is the first value that the following code prints?
Checkpoint5.33.8.
Q-7: What is last value that that following code prints?
Checkpoint5.33.9.
Run this code to see what it prints.
Note5.33.10.
The range(end) function will produce values from 0 to end - 1.
Checkpoint5.33.11.
Q-9: What is first value that that following code prints?
Checkpoint5.33.12.
Q-10: What is last value that that following code prints?
Checkpoint5.33.13.
Run this code to see what it prints.
Note5.33.14.
The function range(start, end) will return a range object (an iterator) that allows you to loop from start (inclusive) to end (exclusive).
Checkpoint5.33.15.
Q-12: What is first value that that following code prints?
Checkpoint5.33.16.
Q-13: What is last value that that following code prints?
Checkpoint5.33.17.
Run this code to see what it prints.
Checkpoint5.33.18.
Drag the blocks from the left and put them in the correct order on the right to define a function total_at_odd_indices that returns the total of the numbers at odd indices in the passed list.
def total_at_odd_indices(alist):
---
def total_at_odd_indices(alist) #paired
---
total = 0
---
for i in range(1,len(alist),2):
---
for i in range(1,len(alist)): #paired
---
total += alist[i]
---
total += i #paired
---
return total
Subsection5.33.3While Loops
A while loop repeats while a Boolean expression is True.
Checkpoint5.33.19.
Try running the code below.
Checkpoint5.33.20.
Q-17: What keyword is used to stop the loop in the above code?
What do you think would happen if you deleted lines 6 and 7 in the above code?
Note5.33.21.
A loop that never ends is called an infinite loop. A while loop should have some way to end. If you have an infinite loop you may need to refresh the page to stop it.
Checkpoint5.33.22.
Run this code to see what it prints.
Modify the code above to keep a count of the number of guesses and print the number of guesses it took to guess the correct value.
Checkpoint5.33.23.
Fix the code below to print from start (inclusive) to 0 and then “Blastoff”
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=func_loop_groupsub data-size_limit=4> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.</p> </div> </div>