5.6. Iteration

5.6.1. The while statement, creating a table (part 1)

In a previous section, you created a program (manning.py) calculating the flow velocity of surface water as a function of the slope, the hydraulic radius, and the Manning roughness coefficient. If you have deleted it, it is given in the table below.

# hydraulic radius (m)
hydraulic_radius = 3.0
# slope (-)
slope = 0.1
# manning's n
n = 0.01
# flow speed
v = (((hydraulic_radius)**(2.0/3.0))*(slope**(1.0/2.0)))/n
# print flow speed
print(v)
# print flow speed with sentence
print("With a hydraulic radius of", hydraulic_radius, "(m) the flow speed is", v, "m/s.")

The program returns the flow velocity for a single set of inputs only. Here we will go a step further. Assume a table is needed that prints the flow velocity for different values of the slope, as shown in the table below (note that the velocity values are not the correct values).

hydraulic radius is: 2.9 (m)
mannings n is: 0.01 (-)

slope (-) velocity (m/s)
0.01      17.435990575118256
0.02      17.259433675623676
0.03      18.12296889834665
0.04      21.17198115023651
0.05      32.87265731575946
0.06      45.91280032308756
0.07      49.803973725916286

To create a program that generates the table above, it is wise first to rewrite the program manning.py by wrapping the manning’s equation in a function with three arguments: the slope, the hydraulic radius, and the manning’s n. The function should return the flow velocity. To do this, open manning.py (or copy-paste it from the table above) and save it as man_func.py. Wrap the code in a function. When running man_func.py, it should create the same output as manning.py! Test this before answering the question below.


Question If the function is defined as def manning(slopeIn, hydraulic_radiusIn,nIn) the names of the variables at the top of the script (e.g., slope, in slope = 3.0) also need to be changed to these same names (e.g., slopeIn), and when calling the function, the same names have to be used (e.g. slopeIn).

  1. Yes

  2. No

Correct answers: b.

Feedback: No, the names used in the function can be different from the names of the variables that are passed to the function. Both versions of the script below will work.

slope = 3.0
hydraulic_radius = 2.9
n = 0.01

def manning(slope, hydraulic_radius,n):
    # flow speed
    v = (((hydraulic_radius)**(2.0 / 3.0)) * (slope**(1.0 / 2.0))) / n
    return v

velocity = manning(slope,hydraulic_radius, n)

# print flow speed
print("With a hydraulic radius of", hydraulic_radius, "(m) the flow speed is", velocity, "m/s.")
slope = 3.0
hydraulic_radius = 2.9
n = 0.01

def manning(slopeIn, hydraulic_radiusIn,nIn):
    # flow speed
    v = (((hydraulic_radiusIn)**(2.0 / 3.0)) * (slopeIn**(1.0 / 2.0))) / nIn
    return v

velocity = manning(slope,hydraulic_radius, n)

# print flow speed
print("With a hydraulic radius of", hydraulic_radius, "(m) the flow speed is", velocity, "m/s.")

The next step is to modify the program such that it prints just the first column. Open man_func.py and save it as man_tab1.py. Keep all lines in the program, except the two lines that calculate and print the flow velocity. Remove these, or de-activate them by changing them into comments:

# velocity = manning(slope,hydraulic_radius, n)
#
# # print flow speed
# print("With a hydraulic radius of", hydraulic_radius, "(m) the flow speed is", velocity, "m/s.")

This allows you to copy-paste this part of the program later, in the case you need it again. Now, add a while statement such that the program prints the table below (i.e. the header and just the first column of what we need as output). You will also need some print statements that print the header (i.e. the lines above the actual table). To print a white line, use the character \n in a string.

hydraulic radius is: 2.9 (m)
mannings n is: 0.01 (-)
slope (-)
0.01
0.02
0.03
0.04
0.05
0.06
0.07

Question Can a variable that is used inside a while statement be used below the while statement, that is outside the statement?

  1. Yes

  2. No

  3. Depends on the name of the variable

Correct answers: a.

Feedback: Yes, unlike variables inside functions, which are local, that is, they can only be used inside the function, variables in a while statement are global, and they can be accessed also outside the while loop. Try it out yourself by adding a line (second script below).

hydraulic_radius = 2.9
n = 0.01

def manning(slope, hydraulic_radius,n):
    # flow speed
    v = (((hydraulic_radius)**(2.0 / 3.0))*(slope**(1.0 / 2.0))) / n
    return v

print("hydraulic radius is:", hydraulic_radius, "(m)")
print("mannings n is:", n, "(-)\n")

print("slope (-)")

x = 0.01

while x < 0.08:
    print(x)
    x = x + 0.01
hydraulic_radius = 2.9
n = 0.01

def manning(slope, hydraulic_radius,n):
    # flow speed
    v = (((hydraulic_radius)**(2.0 / 3.0))*(slope**(1.0 / 2.0))) / n
    return v

print("hydraulic radius is:", hydraulic_radius, "(m)")
print("mannings n is:", n, "(-)\n")

print("slope (-)")

x = 0.01

while x < 0.08:
    print(x)
    x = x + 0.01

print("x can be printed below the loop:", x)
 
  

Finally, extend the program to calculate the flow speed for each slope value (using the manning function created at the start of this exercise), and prints it as a second column (as shown in the table at the start of this section). You can get a nicely formatted table using a tab or multiple tabs between the column. A tab is printed with the string character ‘\t’. Save it as man_tab2.py.


Question What is the velocity (m/s) at a slope of 0.03 and 0.05, respectively?

  1. 24.62, 25.23

  2. 22.23, 22.34

  3. 35.22, 45.47

  4. 20.33, 35.22

Correct answers: c.

Feedback:

hydraulic_radius = 2.9
n = 0.01

def manning(slope, hydraulic_radius,n):
    # flow speed
    v = (((hydraulic_radius)**(2.0 / 3.0))*(slope**(1.0 / 2.0))) / n
    return v

print("hydraulic radius is:", hydraulic_radius, "(m)")
print("mannings n is:", n, "(-)\n")

5.6.2. The while statement, creating a table (part 2)

In the previous section, you made a table of flow velocity values for different slope values. Actually, the flow velocity is a function of two variables, slope and hydraulic radius. So, a table like the one below is needed, giving the velocity for different combinations of slope and hydraulic radius. In this exercise, you will modify the program such that it generates the table below. Again the values for velocity are not correct for the settings required.

mannings n is: 0.01 (-)

slope (-) hydr. radius (m)  velocity (m/s)
0.11      3.0               68.98857573551179
0.11      4.0               83.57370775943878
0.11      5.0               96.97869717995717
0.12      3.0               72.05621731056017
0.12      4.0               87.28989087773772
0.12      5.0               101.29094569634633
0.13      3.0               74.99848881276804
0.13      4.0               90.85419896864778
0.13      5.0               105.42695885492729
0.14      3.0               77.92961001631138
0.14      4.0               94.28385806182264
0.14      5.0               109.40672569242204
0.15      3.0               80.5613000539548
0.15      4.0               97.59306487558015
0.15      5.0               113.24672004113509
0.16      3.0               83.20335292207616
0.16      4.0               100.79368399158986
0.16      5.0               116.96070952851464

Create the program for generating the table in two steps. Open man_tab2.py and save it as man_tab3.py. Modify man_tab3.py such that it generates the table below. If you have problems solving this, first answer the first question below the table.

mannings n is: 0.01 (-)

slope (-) hydr. radius (m)
0.11      3.0
0.11      4.0
0.11      5.0
0.12      3.0
0.12      4.0
0.12      5.0
0.13      3.0
0.13      4.0
0.13      5.0
0.14      3.0
0.14      4.0
0.14      5.0
0.15      3.0
0.15      4.0
0.15      5.0
0.16      3.0
0.16      4.0
0.16      5.0

Question How do you think you can generate the table given above?

  1. Using two while loops below each other.

  2. Using two while loops, the second one nested in the body of the first one.

  3. Using a while loop, with an if statement inside the while loop.

Correct answers: b.

Feedback:

n = 0.01

def manning(slope, hydraulic_radius,n):
 # flow speed
 v = (((hydraulic_radius)**(2.0/3.0))*(slope**(1.0/2.0)))/n
 return v

print( "mannings n is:", n, "(-)\n")

print("slope (-)\thydr. radius (m)")
x = 0.1
while x < 0.16:
  x = x + 0.01
  y = 2.0
  while y < 4.1:
    y = y + 1
    print(x, "\t\t", y)

Now, save man_tab3.py as man_tab4.py and change it resulting in an output including flow velocities that corresponds to the table given at the top of this section.


Question What is the velocity (m/s) at a slope of 0.14 and a hydraulic radius of 3.0 m?

  1. 89.12

  2. 46.83

  3. 77.92

  4. 77.82

Correct answers: d.

Feedback:

n = 0.01

def manning(slope, hydraulic_radius,n):
 # flow speed
 v = (((hydraulic_radius)**(2.0/3.0))*(slope**(1.0/2.0)))/n
 return v

print( "mannings n is:", n, "(-)\n")

print("slope (-)\thydr. radius (m)\tvelocity (m/s)")
x = 0.1
while x < 0.16:
  x = x + 0.01
  y = 2.0
  while y < 4.1:
    y = y + 1
    velocity = manning(x,y, n)
    print(x, "\t\t", y, "\t\t\t", velocity)