Zybooks Chapter 5 Loops Participation Activity Questions
6
Given the following code, how many times will the inner loop body execute? for i in range(2): for j in range(3): # Inner loop body
10
Given the following code, how many times will the print statement execute? for i in range(5): for j in range(10, 12): print(f'{i}{j}')
1
Given the following while loop, what is the value variable z is assigned with for the given values of variables a, b and c? mult = 0 while a < 10: mult = b * a if mult > c: break a = a + 1 z = a a = 1, b = 1, c = 0
5
Given the following while loop, what is the value variable z is assigned with for the given values of variables a, b and c? mult = 0 while a < 10: mult = b * a if mult > c: break a = a + 1 z = a a = 4, b = 5, c = 20
False
Given: for i in range(5): if i < 10: continue print(i) 1) The loop will iterate only once. True False
False
Given: for i in range(5): if i < 10: continue print(i) 1) The loop will print at least some output. True False
True
Incremental programming may help reduce the number of errors in a program. True False
for
Iterate 1500 times while for
while
Iterate as long as the user-entered string c is not q. while for
my_pet
Iterate over the given list using a variable called my_pet. for ___ in ['Scooter', 'Kobe', 'Bella']:
price in my_prices
Iterate over the list my_prices using a variable called price. for ___:
number in '911'
Iterate the string '911' using a variable called number. for ___:
while c != 'z'
Iterate until c equals 'z'.
while
Iterate until the values of x and y are equal, where x and y are changed in the loop body. while for
while c == 'g'
Iterate while c equals 'g'.
x >= 0
Iterate while x is greater than or equal to 0.
while x < 100
Iterate while x is less than 100.
i = -100
Loop iterates from -100 to 65. ___ while i <= 65: # Loop body statements go here i = i + 1
i <= 1000
Loop iterates over multiples of 5 from 0 to 1000 (inclusive). i = 0 while ___: # Loop body statements go here i = i + 5
i = i + 2
Loop iterates over the odd integers from 1 to 9 (inclusive). i = 1 while i <= 9: # Loop body statements go here ___
i = i - 2
Loop iterates over the odd integers from 211 down to 31 (inclusive). i = 211 while i >= 31: # Loop body statements go here ___
False
Once a program is complete, a programmer can expect to see several FIXME comments. True False
reversed(scores)
Print scores in order from highest to lowest. Note: List is pre-sorted from lowest to highest. scores = [75, 77, 80, 85, 90, 95, 99] for scr in ___: print(scr, end=' ')
aa ab ac
What is the output of the following code? c1 = 'a' while c1 < 'b': c2 = 'a' while c2 <= 'c': print(f'{c1}{c2}', end = ' ') c2 = chr(ord(c2) + 1) c1 = chr(ord(c1) + 1)
13 16 19 113 116 119
What is the output of the following code? i1 = 1 while i1 < 19: i2 = 3 while i2 <= 9: print(f'{i1}{i2}', end=' ') i2 = i2 + 3 i1 = i1 + 10
2 3 4
What sequence is generated by range(2, 5)? 2 3 4 2 3 4 5 0 1 2 3 4
0 1 2 3 4 5 6
What sequence is generated by range(7)? 0 1 2 3 4 5 6 7 1 2 3 4 5 6 0 1 2 3 4 5 6
my_var *= 2
Write a statement using *= that doubles the value of a variable my_var.
my_var += my_var / 2
Write a statement using += that is equivalent to my_var = my_var + my_var / 2
0 Greek 1 Nordic 2 Mayan
for (index, value) in enumerate(my_list): print(index, value) If my_list = ['Greek', 'Nordic', 'Mayan'], what is the output of the program? Greek Nordic Mayan 0 Greek 1 Nordic 2 Mayan 1 Greek 2 Nordic 3 Mayan
1 2 3
x = 1 y = 3 z = 5 while not (y < x < z): print(x, end=' ') x = x + 1
IL
x = 10 while x != 3: print(x, end=' ') x = x / 2
3
x = 3 while x >= 1: # Do something x = x - 1
18 13 8
x = 5 y = 18 while y >= x: print(y, end=' ') y = y - x
num_neg += 1
Assign num_neg with the number of below-freezing Celsius temperatures in the list. temperatures = [30, 20, 2, -5, -15, -8, -1, 0, 5, 35] num_neg = 0 for temp in temperatures: ___ if temp < 0:
0
Assume the user would enter 'n', then 'n', then 'y'. # Get character from user here while user_char != 'n': # Do something # Get character from user here
2
Assume user would enter 'a', then 'b', then 'n'. # Get character from user here while user_char != 'n': # Do something # Get character from user here
No
Complete the program so the variable max ends up having the maximum value in an input list of positive values (the list ends with 0). So if the input is 22 5 99 3 0, then max should end as 99. max = -1 val = Get next input while val is not 0 If __(A)__ __(B)__ val = Get next input Does the final value of max depend on the order of inputs? In particular, would max be different for inputs 22 5 99 3 0 versus inputs 99 3 5 22 0? Yes No
-1, 5, 10, 20
Complete the program so the variable max ends up having the maximum value in an input list of positive values (the list ends with 0). So if the input is 22 5 99 3 0, then max should end as 99. max = -1 val = Get next input while val is not 0 If __(A)__ __(B)__ val = Get next input For inputs 5 10 7 20 8 0, with what values should max be assigned? -1, 20 -1, 5, 10, 20 -1, 5, 10, 7, 20
val > max
Complete the program so the variable max ends up having the maximum value in an input list of positive values (the list ends with 0). So if the input is 22 5 99 3 0, then max should end as 99. max = -1 val = Get next input while val is not 0 If __(A)__ __(B)__ val = Get next input What should expression (A) be? max > 0 max > val val > max
max = val
Complete the program so the variable max ends up having the maximum value in an input list of positive values (the list ends with 0). So if the input is 22 5 99 3 0, then max should end as 99. max = -1 val = Get next input while val is not 0 If __(A)__ __(B)__ val = Get next input What should statement (B) be? max = val val = max max = max + 1
val < 0
Complete the program such that variable count ends having the number of negative values in an input list of values (the list ends with 0). So if the input is -1 -5 9 3 0, then count should end with 2. count = 0 val = Get next input While val is not 0 if ___(A)__ __(B)__ val = Get next input What should expression (A) be? val > 0 val < 0 val is 0
No
Complete the program such that variable count ends having the number of negative values in an input list of values (the list ends with 0). So if the input is -1 -5 9 3 0, then count should end with 2. count = 0 val = Get next input While val is not 0 if ___(A)__ __(B)__ val = Get next input If the input value is 0, does the loop body execute? Yes No
count = count + 1
Complete the program such that variable count ends having the number of negative values in an input list of values (the list ends with 0). So if the input is -1 -5 9 3 0, then count should end with 2. count = 0 val = Get next input While val is not 0 if ___(A)__ __(B)__ val = Get next input What should statement (B) be? val = val + 1 count = count + 1 count = val
num
Compute the average number of kids. # Each list item is the number of kids in a family. num_kids = [1, 1, 2, 2, 1, 4, 3, 1] total = 0 for num in num_kids: total += ___ average = total / len(num_kids)
range(10, 21, 2)
Every 2nd integer from 10 to 20
range(501)
Every integer from 0 to 500.
range(10, 21)
Every integer from 10 to 20
range(5, -6, -1)
Every integer from 5 down to -5
True
FIXME comments provide a way for a programmer to remember what needs to be added. True False
5
Use input values of num_a = 15 and num_b = 10 in the above GCD program. Answer the questions by mentally executing the statements. If stuck, consider adding additional print statements to the program. What is num_b after the second and before the third iteration?
15
Use input values of num_a = 15 and num_b = 10 in the above GCD program. Answer the questions by mentally executing the statements. If stuck, consider adding additional print statements to the program. What is the value of num_a before the first loop iteration?
5
Use input values of num_a = 15 and num_b = 10 in the above GCD program. Answer the questions by mentally executing the statements. If stuck, consider adding additional print statements to the program. What is num_a after the first iteration and before the second?
i <= 10
The loop iterates 10 times. i = 1 while ___: # Loop body statements go here i = i + 1
i <= 2
The loop iterates 2 times. i = 1 while ___: # Loop body statements go here i = i + 1
i <= 99
The loop iterates 99 times. i = 1 while ___: # Loop body statements go here i = i + 1
2
Use input values of num_a = 15 and num_b = 10 in the above GCD program. Answer the questions by mentally executing the statements. If stuck, consider adding additional print statements to the program. How many loop iterations will the algorithm execute?
Bye
x = 0 while x > 0: print(x, end=' ') x = x - 1 print('Bye')
x == y
x = 0 y = 5 z = ? while x < y: if x == z: print('x == z') break x += 1 else: print('x == y') What is the output of the code if z is 10? x == z x == y
x == z
x = 0 y = 5 z = ? while x < y: if x == z: print('x == z') break x += 1 else: print('x == y') What is the output of the code if z is 3? x == z x == y