We've spent some time going into detail about some of the data types and structures available in python. It's now time to talk about how to navigate through some of this data, and use data to make decisions.
Traversing over data and making decisions based upon data are a common aspect of every programming language. To understand how to do this, we will study the concept of iteration. Iterations allow us to execute the same task multiple times, and is a key programming concept.
# This is just to use the time.sleep function
# We will use the time.sleep function to slow
# down the execution of loops, only for
# instructional purposes. In reality, we add
# such delays only in special cases.
import time
The while
statement provides a general way for creating iterative code execution.
As in the case of the if
statement, the while
statement uses a boolean expression that controls the flow of execution.
The difference is that, when we use the if
statement, the nested code in executed only once; when we use the while
statement, the nested code keeps executing, as long as the boolean expression is True
.
For example, the following example counts until i
becomes larger than 5
i = 0
while i <= 5: # check if i<=5. If yes, execute the nested block of code
print(i) # print the current value of i
time.sleep(1.0) # Wait for 1 sec
i = i + 1 # increase i
# Now go to the beginning of the loop
More formally, here is the flow of execution for a while
statement:
False
or True
.False
, exit the while
statement and continue execution at the next statement that is after the body of the while.True
, execute each of the statements in the body and then go back to step 1.The body consists of all of the statements below the header with the same indentation.
This type of flow is called a loop because the third step loops back around to the top. Notice that if the condition is False
the first time through the loop, the statements inside the loop are never executed.
Or consider the following example: The tea starts at 115 degrees Fahrenheit. You want it at 110 degrees. A chip of ice turns out to lower the temperature one degree every second. You test the temperature each time, and also print out the temperature before reducing the temperature. In Python you could write and run the code below:
temperature = 115
while temperature > 110: # Execute the loop code if temperature > 110
print(temperature)
time.sleep(1.0) # Wait for 1 sec
temperature = temperature - 1 # Decrease the value of temperature by one
# Now go back to the beginning of the loop
print("The tea is cool enough.")
Notice that the body of the loop should change the value of one or more variables so that eventually the condition becomes False
and the loop terminates. If the condition never becomes False, the loop will repeat forever. Such type of a loop is called an infinite loop.
And here is a simplified simulator that starts with an amount of money in the bank, and then examines the effect of withdrawing a specific amount of money per year, until you run out of money. Notice that the loop will keep running for ever, if you never withdraw more money than what you have. (We will deal with this issue next.)
money_in_bank = 1000
interest = 6
year = 2017
widthdrawal_per_period = 200
while money_in_bank > 0:
print(f"At the beginning of {year} you have ${money_in_bank:.2f} in the bank.")
money_in_bank = money_in_bank - widthdrawal_per_period
money_in_bank = money_in_bank * (1 + interest / 100)
year = year + 1
print(f"At the end of {year} you have ${money_in_bank:.2f} in the bank.")
print("-----------------")
print("You have no money left!")
The following code contains an infinite loop.
n = 10
answer = 1
while n > 0:
answer = answer + n
n = n + 1
print(answer)
Which is the best explanation for why the loop does not terminate?
n
starts at 10 and is incremented by 1 each time through the loop, so it will always be positiveanswer
starts at 1 and is incremented by n
each time, so it will always be positiven
to 0 in while
loop. You must compare it to another variable.while
loop body, we must set n
to False
, and this code does not do that.You may have seen that Python’s while
is very close to the English “while”. There is an important difference, though: In English “while X holds, do Y”, we usually assume that immediately after X becomes false, we stop with Y. In Python there is not an immediate stop: Even if the condition becomes false in the middle of the loop body, the rest of the loop body will still be executed, and the loop will stop only before the next iteration.
Break
and Continue
¶Let's discuss now two commands, break
and continue
, that allow us to control better the execution of code within a loop.
These two statements are used to modify iteration of loops. break
is used to exit immediately the inner most loop in which it appears. In contrast, continue
stops the code executing within the loop and goes on to the next iteration of the same loop.
For example, consider our example with the bank account. The loop will keep running for ever, if you never withdraw more money than what you have.
To avoid this infinite loop, we can add an extra check in the code, checking if the year is above a certain limit, and stop execution of the loop at that point.
money_in_bank = 1000
interest = 6
year = 2017
# Try also the values 56.6 and 56.7 and see its behavior
widthdrawal_per_period = 50
while money_in_bank > 0:
print(f"At the beginning of {year} you have ${money_in_bank:.2f} in the bank")
money_in_bank = money_in_bank - widthdrawal_per_period
money_in_bank = money_in_bank * (1 + interest / 100)
year = year + 1
if year > 2117:
print("I am pretty sure you will not be alive by then")
break
print(f"At the end of {year} you have ${money_in_bank:.2f} in the bank")
print("-----------------")
print("You have no money left (or you are dead)!")
Here is another example, this time with the continue
command. In the example below, we modify our "tea cooling" example to print the temperature only when temperature is exactly divisible by 5. If not, we use the continue
command to skip executing the rest of the loop.
import time
temperature = 133
while temperature > 110: # first while loop code
temperature = temperature - 1
if temperature % 5 != 0: # If the temperature is not divisible by 5
continue # We keep running the loop, but will not print
# the temperature and have a delay
time.sleep(0.5)
print(temperature)
print("The tea is cool enough.")
Write a program that receives user for three pieces of information as variables:
The program then outputs the number of years required for the starting balance to have grown larger than the target balance. While this can be computed directly mathematically, we want for this exercise to use a while loop to figure out the answer. The answer should just be a line stating something like: "To grow an initial investment of \$1000 to \$2000 at 5.0% will require XX
years".
Hint: You will need a variable to store the number of years that passed. You will also need a variable to hold the current balance in the account, as it grows over the years.
starting = 1000
target = 2000
interest = 0.05
# your code here
starting = 1000
target = 2000
interest = 0.045
# We introduce a variable to store the current amount of money we have
current = starting
# We also introduce a variable year to count the number of years that passed
year = 0
# We will keep increasing the current balance, year after year, until
# the value of current surpases the target
while current < target:
# We increase the value of current, by adding interest
current = current * (1 + interest)
# Alternatively, you can write the above as
# current += current * interest
# current = current + current * interest
# current *= 1+interest
# We increase the year value, as a year has passed
year = year + 1
# We also print a message with the current balance
# as this can be useful for debugging
print(f"After {year} years, you have ${current:.2f}")
# We are out of the loop, let's print how long it took:
print(
f"To grow an initial investment of ${starting} to ${target} at {100*interest:.1f}% will require {year} years"
)