Note 5.12.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.
for
loops, which iterate over the items of a sequence, and while
loops, which continue to execute as long as a condition is true.for
statement.range()
works given 1, 2, or 3 arguments.while
loop.for
loop executes the same block of code “for each item in a sequence.”for
loop?for
loop executes as many times as there are items in a sequence. Try again.for
loop executes as many times as there are items in a sequence. Try again.for
loop executes as many times as there are items in a sequence. Try again.for
loop executes 3 times.for
loop, so line 4 does not count. Try again.for
loop?for
loop terminates (finishes executing), the program continues to execute the non-indented lines of code beneath it. Try again.for
loop and therefore does not execute multiple times.for
loop. Try again.for
loop. Try again.for
loop. Try again.x
is selected from the list. Each time the loop runs, the next value from the list is assigned to x
.for
statement, you can assign your list to a variable and your program will run the same way:range
function will generate a sequence of numbers. The range
function can take up to three numbers as arguments.list
function.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.range
function with one parameter to generate the sequence 0, 1, 2, 3.
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.range
function with two parameters to generate the sequence 1, 2, 3, 4.
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. The sequence will generate numbers up to, but not including, y
. 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.range
function with three parameters to generate the sequence 1, 3, 5, 7.
for
statement is the most appropriate to use?for i in range(x)
for i in range(x, y)
for i in range(x, y, z)
for i in list
numbers
inside the loop, which type of for
statement should you use?for i in range(x)
range
function. Try again.for i in range(x, y)
range
function. Try again.for i in range(x, y, z)
range
function. Try again.for i in numbers
while
statement.while
) be in order for the first print
statement to execute?while
loop will execute as long as the loop condition is True.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
becomes 3, which causes the loop body to stop executing.i = 0
while i < 3:
i = i + 1
print(i)
print("goodbye")
i
is incremented before it is printed, so the numbers it prints are one higher than before.i < 3
is False
). Try again.i < 3
is False
). Try again.i = 0
while i < 3:
print(i)
i = i + 1
print("goodbye")
i = 1
1 2
.i < 2
0 1
.i = i + 2
0 2
.NameError
because i
wouldn’t be defined when the program tries to run the while
line for the first time. Try again.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.i
does not increase anywhere in the code. Try again.i
begins at 0, not 1. Try again.i
never changes, the program will never leave the while
loop.while
loop, it’s helpful to answer a few questions before you start:n
numbers and returns the sum of these values. For example, when n = 5
, the user is asked to input five numbers. If the user inputs 30, 10, 50, 20, and 40, the function would return the value 150.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.n
times, so the Boolean expression that must be true for the loop to continue is i < n
.i
must be incremented, so we include the statement i = i + 1
.while
loops.https://cspogil.org/Home