number = 5
while number <= 5:
if number < 5:
number = number + 1
print(number)
The program will loop indefinitely
Correct! This code loops while number is less than or equal to 5. number only increments if it is less than 5, and itβs originally set to 5, so βnumberβ never changes.
The value of number will be printed exactly 1 time
Incorrect! This would be true if line 3 said "if number <= 5:". Try again.
The while loop will never get executed
Incorrect! This would be true if number was initialized as a number larger than 5 to start. Try again.
The value of number will be printed exactly 5 times
Incorrect! This would be true if number was initialized as 0. Try again.
counter = 1
sum = 0
while counter <= 6:
sum = sum + counter
counter = counter + 2
print(sum)
12
Incorrect! This would be true if counter was initialized as 0. Try again.
9
Correct! This loop executes 3 times. After the first loop sum = 1 and counter = 3, after the second loop sum = 4 and counter = 5, and after the third loop sum = 9 and counter = 7.
7
Incorrect! This is the value of counter, but this code prints the value of sum. Try again.
8
Incorrect! This would be the value of counter after the loop if counter was initialized as 0. Try again.
Which type of loop can be used to perform the following iteration: You choose a positive integer at random and then print the numbers from 1 up to and including the selected integer.
Correct! Although you do not know how many iterations you loop will run before the program starts running, once you have chosen your random integer, Python knows exactly how many iterations the loop will run, so either a for-loop or a while-loop will work.
only a for-loop
Incorrect! As you learned in section 7.2, a while-loop can always be used for anything a for-loop can be used for. Try again.
only a while-loop
Incorrect! Although you do not know how many iterations you loop will run before the program starts running, once you have chosen your random integer, Python knows exactly how many iterations the loop will run, so this is an example of definite iteration. Try again.
Incorrect! This will be an infinite loop as the value of i never changes.
i = 0
while(i < n)
<body>
i = i + 1
Correct! The value of i increments by 1 in each iteration till it becomes equal to n at which point the loop condition wonβt be satisfied.
i = 0
while(i < n)
<body>
n = n + 1
Incorrect! This is not the right implementation of the given for loop as the value of i remains the same and the value of n keeps increasing with each iteration.
i = 1
while(i < n)
<body>
i = i + 1
Incorrect! This is not the right implementation of the given for loop as the value of i remains the same and the value of n keeps increasing with each iteration.