Lots of times in climate modeling, we want to check whether some condition is met before going ahead. Python does this with an if block, which looks like
if (some condition is met):
line1
line2
...
This is not actually Python code -- it's what we call pseudo code because it's supposed to be a little more intuitive than actual code$^*$. The point for now is, the entire set ("block") of indented commands line1, line2, ... will be executed only if that condition is met.
$*$That said, some people refer to Python as "executable pseudo code."
A similar situation also arises when we want to do something over and over again, like march our climate model forward over time, and make that also contingent on meeting some kind of condition. That kind of thing calls for a loop. Among the many ways to set up a loop, the one we'll explore here is called a while loop (sometimes referred to as a pre-test loop). It looks a lot like the if syntax:
while (some condition is met):
line1
line2
...
only now, those lines will be executed over and over again. If we've set things up right, the condition eventually stops being true, and we exit from the loop. while loops often require a little more setup, and are often followed by more reporting, than if statements -- but we'll talk about that later.
An emergency exit from a loop is a kind of failsafe that you build into your loop, in case something goes wrong and the condition you thought would terminate the loop is never met. Yeah, maybe your loop will never trigger it, but everybody makes mistakes (that's why we call it an emergency exit). In this exercise, you'll be equipping a while loop with an emergency exit using a nested if block -- i.e., an if condition that's inside the loop.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
Below is an example of an if statement. You'll see that the test of whether two numbers are equal is a little unusual -- Python uses "==" instead of "=". The signs ">" and "<" work as expected, however. Check it out.
print("testing whether 2+2<4")
if 2+2<4:
print("This should not print because 2+2 is not less than 4")
print("have a nice day")
print("testing whether 2+2=4")
if 2+2==4:
print("This should print because 2+2 really does equal 4")
print("have a nice day")
testing whether 2+2<4 testing whether 2+2=4 This should print because 2+2 really does equal 4 have a nice day
In the cell below, set up three if-blocks like the ones above, but testing these conditions:
Obviously, only the last should print stuff out.
### BEGIN SOLUTION
print("testing whether 2+2<3")
if 2+2<3:
print("Yup")
print("have a nice day")
print("testing whether 2+2=3")
if 2+2==3:
print("yup")
print("have a nice day")
print("testing whether 2+2>3")
if 2+2>3:
print("Yup")
print("have a nice day")
### END SOLUTION
testing whether 2+2<3 testing whether 2+2=3 testing whether 2+2>3 Yup have a nice day
Below is an example of a "while" loop. You can think of it as being in three parts:
# Loop control
my_number = 0
big_enough = 30
# Looping
while my_number < big_enough:
my_number += 5
print(my_number)
# Reporting
print("After the loop, my_number=", my_number)
5 10 15 20 25 30 After the loop, my_number= 30
In the cell below, set up a "while" loop that does the same thing, but with some differences:
### BEGIN SOLUTION
# Loop control
my_number = 10
big_enough = 60
# Looping
while my_number < big_enough:
my_number += 15
print(my_number)
# Reporting
print("After the loop, my_number=", my_number)
### END SOLUTION
25 40 55 70 After the loop, my_number= 70
Did the output of the loop you just executed surprise you? Write a sentence or two in the cell below explaining why you think my_number got above big_enough. (If you'd like a more general perspective on this, check out https://en.wikipedia.org/wiki/While_loop).
Sometimes, instead of just printing a variable to the screen as we go through a loop, we'd like to accumulate information as we go along. Here we'll do this with a Python list.
What's a Python list? It's a set of numbers enclosed in square brackets**. Lists can be empty, which is how we set things up before the loop starts. Then, inside the loop, numbers are appended to it. The key command in the loop below is
my_number_list.append(my_number)
Execute the cell below to see how this works.
**Python lists can actually contain character strings too, but here we're just going to make lists of numbers.
# Loop control
my_number = 0
big_enough = 30
# Starting out with an empty list
my_number_list = []
# Looping
while my_number < big_enough:
my_number += 5
my_number_list.append(my_number)
print(my_number_list)
# Reporting
print("After the loop, my_number_list=", my_number_list)
[5] [5, 10] [5, 10, 15] [5, 10, 15, 20] [5, 10, 15, 20, 25] [5, 10, 15, 20, 25, 30] After the loop, my_number_list= [5, 10, 15, 20, 25, 30]
In the cell below, duplicate your "while" loop (starting at 0, max value of 100, increment of 10), but accumulating my_number in a list. Name your list my_number_list.
### BEGIN SOLUTION
# Loop control
my_number = 0
big_enough = 100
# Starting out with an empty list
my_number_list = []
# Looping
while my_number < big_enough:
my_number += 10
my_number_list.append(my_number)
print(my_number_list)
# Reporting
print("After the loop, my_number_list=", my_number_list)
### END SOLUTION
[10] [10, 20] [10, 20, 30] [10, 20, 30, 40] [10, 20, 30, 40, 50] [10, 20, 30, 40, 50, 60] [10, 20, 30, 40, 50, 60, 70] [10, 20, 30, 40, 50, 60, 70, 80] [10, 20, 30, 40, 50, 60, 70, 80, 90] [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] After the loop, my_number_list= [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Sometimes, you make a mistake, and the condition set up to terminate your loop is never reached! You just sit there, waiting for you loop to finish, and it never does. It happens to everybody sooner or later.
So, it's often useful to add an emergency exit to your loop. Yeah, maybe your loop will never need it ... but that's why we call it an emergency exit.
In the cell below, the emergency exit is triggered when the number of items in the list we're generating (calle the length of the list) gets bigger than 4. The exit itself is accomplished with the "break" key word, which is inside an "if" block, which is inside a while loop.
# Loop control
my_number = 0
big_enough = 30
# Starting out with an empty list
my_number_list = []
# Looping
while my_number < big_enough:
my_number += 5
my_number_list.append(my_number)
print(my_number_list)
# Emergency exit
if (len(my_number_list) > 4):
print('Emergency exit!')
break
# Reporting
print("After the loop, my_number_list=", my_number_list)
[5] [5, 10] [5, 10, 15] [5, 10, 15, 20] [5, 10, 15, 20, 25] Emergency exit! After the loop, my_number_list= [5, 10, 15, 20, 25]
In the cell below, duplicate the "while" loop you created three cells above (the loop that starts at 0, with a max value of 100, increment of 10, accumulating my_number in my_number_list), but add an emergency exit if the length of my_number_list exceeds 5 items.
### BEGIN SOLUTION
# Loop control
my_number = 0
big_enough = 100
# Starting out with an empty list
my_number_list = []
# Looping
while my_number < big_enough:
my_number += 10
my_number_list.append(my_number)
print(my_number_list)
if (len(my_number_list) > 5):
print('Emergency exit!')
break
# Reporting
print("After the loop, my_number_list=", my_number_list)
### END SOLUTION
[10] [10, 20] [10, 20, 30] [10, 20, 30, 40] [10, 20, 30, 40, 50] [10, 20, 30, 40, 50, 60] Emergency exit! After the loop, my_number_list= [10, 20, 30, 40, 50, 60]
When a list is short, it's fine to print it out in its entirety, as we've done. But if a list gets too long, there's no point in looking at all that. In such situations, it's often to useful to examine the first and the last element of the list.
In the example below, we create a list, called my_football_list, that contains a combination of numbers and character strings (it's supposed to be what might be said at a football scrimmage line, in case you're wondering). A couple of notes about this:
Why would we be interested in #3? It's just kind of handy -- it allows us to print the last element of a list without actually knowing how long it is. Here's an example:
my_football_list = [25, 32, 'hut', 'hut', 'hike']
print(my_football_list)
print("First element:", my_football_list[0])
print("Last element:", my_football_list[-1])
[25, 32, 'hut', 'hut', 'hike'] First element: 25 Last element: hike
Using the above syntax, print the first and last elements of the list you created a few cells back, which you named my_number_list.
### BEGIN SOLUTION
print(my_number_list)
print("First element:", my_number_list[0])
print("Last element:", my_number_list[-1])
### END SOLUTION
[10, 20, 30, 40, 50, 60] First element: 10 Last element: 60
Almost done! To double-check everything is OK, repeat the "Three steps for refreshing and saving your code," and press the "Validate" button (as usual).