The problem statement:
We are tasked with displaying all the powers of 2 less than 1,000 along with a message describing the output.
The following is the expected approximate output of program 5:
- 1
- 2
- 4
- 8
- 16
- 32
- 64
- 128
- 156
- 512
- These are all the powers of 2 less than 1000
The Solution:
def powers_of_two_less_than_one_thousand():
power = 1
while power < 1000:
print(power)
power *= 2
print("These are all the powers of 2 less than 1000")
def equivalent_solution():
power = 1
print(power)
power = power * 2
print(power)
power = power * 2
print(power)
power = power * 2
print(power)
power = power * 2
print(power)
power = power * 2
print(power)
power = power * 2
print(power)
power = power * 2
print(power)
power = power * 2
print(power)
power = power * 2
print(power)
print("These are all the powers of 2 less than 1000")
if __name__ == '__main__':
powers_of_two_less_than_one_thousand()
In previous examples we saw how to use a for loop to write concise solutions that allowed us to reuse code that is written in the body of the loop. Our solution to the stated problem will also use a loop to reuse code, but it will not be a for loop because we are not interested in iterating over a list of predefined items. Instead, we use a while loop.
What is a while loop?
A while loop allows us to continue processing code that is within the body of the loop until a condition is met or satisfied. The looping condition for our while loop is that the variable named power is less than the value of 1000. If this condition ever fails then the while loop is terminated.
How does a while loop work?
The body of the loop is executed until the condition becomes false. The condition that is defined is evaluated for a boolean value of True or False at the beginning of each loop. The loop terminates once the evaluation of our conditional "sees" a value of False, else the looping continues. This implies that it is possible to program a while loop that never terminates and therefore runs forever. This condition is known as an infinite loop and is usually the result of a bug in our code or reasoning. We can create create the condition of an infinite loop by forgetting to multiply by 2 on each iteration of the loop.
Unraveling the while loop.
If you are having difficulties understanding the code in this or any while loop then it is sometimes helpful to unravel the loop. We can gain greater visibility in the looping code by examining an equivalent solution that does not use any looping mechanism. That is, solve the solution in its entirety, or partially if the result is long, by not reusing code inside a loop. For example, the solution given with a while loop is equivalent to the code found in the equivalent_solution() function, (remember that power *= 2 is shorthand for power = power * 2).
Page with links to the entire series can be found here
Are you having any issues running or understanding the program? Please, add a comment explaining where you are stuck or where the tutorial is not clear so we can improve it.