Exercises
Contents
Exercises¶
Week 5¶
Conditional execution¶
a. Calculate the absolute value of a number (not using the built-in abs function). Use the following code as a template (replace the dashes —– with code).
x = ------
if -----:
absx = -----
else:
absx = -----
print('the absolute value of x is',absx)
b. Using conditional execution, determine whether a user-provided integer x is even or odd and print the result (hint: the % operator calculates the remainder that occurs when dividing two numbers, e.g. the result of 7 % 3 is equal to 1).
xin = input('Please enter a number:')
x = int(xin)
if ------:
--------
else:
print(‘The number is odd’)
c. Modify the code written in part b to use try and except statements to handle non-integer input gracefully. If the input is not an integer, the program should print a friendly message explaining what went wrong.
d. (if time) Modify the code written in part b above to continue requesting input until an integer is provided.
Grain size classification (if time)¶
Write a function that takes sediment grain size as an input, then returns the Wentworth grain size classification based on the table below.
Grain Size |
Classification |
---|---|
>= 256 mm |
Boulder |
64-256 mm |
Cobble |
2-64 mm |
Gravel |
62.5 μm - 2 mm |
Sand |
3.9-62.5 μm |
Silt |
0.98-3.9 μm |
Clay |
Test the program repeatedly to make sure it works for different input values.