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.
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.
A loop allows you to execute the same statements multiple times. Python has two kinds of loop structures: for loops, which iterate over the items of a sequence, and while loops, which continue to execute as long as a condition is true.
Incorrect! After the "for" loop terminates (finishes executing), the program continues to execute the non-indented lines of code beneath it. Try again.
1
Correct! The not indented line of code executes only once, as it is not part of the "for" loop and therefore does not execute multiple times.
2
Incorrect! The absence of an indent in Line 4 means that it is not part of the "for" loop. Try again.
3
Incorrect! The absence of an indent in Line 4 means that it is not part of the "for" loop. Try again.
4
Incorrect! The absence of an indent in Line 4 means that it is not part of the "for" loop. Try again.
Imagine that the list [2, 7, 1] from the code above was modified to one of the lists below. Match each new list to the amount of times it would make the "for" loop execute.
In general, the length of the list determines the number of times that the loop repeats. The value of the variable x is selected from the list. Each time the loop runs, the next value from the list is assigned to x.
With strings, a for statement iterates over each character in the string. The length of the string determines how many times the body of the loop will run.
The first line of output describes the range as a function, whereas the second line shows the actual range of values as a list by using the list function.
If the argument of the range function specifies a single number, like range(x), the first number listed will be 0, the last number listed will be x - 1, and there will be x numbers in the list.
If the argument of the range function specifies two numbers, like range(x, y), the first number listed will be x, the last number listed will be y - 1, and there will be y - x numbers in the list.
If the argument of the range function specifies three numbers, like range(x, y, z), the first number listed will still be x, just like the two parameter version. The third argument represents how much to increment the number by each time. To calculate how many numbers will be in the list, take the result of (y - x) / z and round it up to the nearest whole number.
The arguments to range must be integers, so range does not work with strings. However, if you wanted to print the letters A to Z in a loop, you could do something like this:
In the above code, the variable i is incremented by 1 each time the loop body is executed. Because the value of i steadily grows, the βloop conditionβ (the Boolean expression after the while) eventually becomes false when i = 3, which causes the loop body to stop executing.
Incorrect! This would cause a NameError because "i" wouldnβt be defined when the program tries to run the "while" line for the first time. Try again.
A while loop has three parts that control the number of times it executes. The first part initializes the variable or condition, the second part tests whether the end has been reached, and the third part updates the variable or condition.
For example, consider the code below. The add(n) function prompts the user for n numbers and returns the sum of these values. For example, when add(5) is called, the user is asked to input five numbers. If the user inputs 3, 1, 5, 2, and 4, the function would return the value 15.
Before the loop begins, the i variable, which counts how many times the loop runs, must be initialized. However, the total variable must also be initialized outside of the while loop, or else it would reset to 0 each time the loop ran.