Practice with Python Control Flow
Control flow is a general term for the order in which a computer
program executes its statements.
The “normal” control flow is to use the first line of code,
then the second, then the third, etc., in sequence. But, we've now
seen several ways to modify this simple sequence.
For example, a loop will generally
repeat its loop body, and an if
will use only one of its
conditional branches. It also includes the idea of calling a function,
then the program will transfer control to the function body and eventually
return.
In short, we're going to practice writing functions that use conditionals or loops. For each of the following problems, read the problem description and examples, define a function to solve the problem, then check your result against our sample solution.
At the end of each semester, instructors have to assign letter grades.
For example, a score of 90 or above is an A, 80 or above is a B,
70 or above is a C, 60 or above is a D, and anything else is an F.
Define a function letterGrade
that takes a score
and returns the letter grade as a string.
-
def letterGrade(score): if score >= 90: return "A" elif score >= 80: return "B" elif score >= 70: return "C" elif score >= 60: return "D" else: return "F"
At the beginning of the semester, we counted how many students
had Apple laptops, Windows laptops, or no laptop.
Define a function countLaptops
that is
given a list such as
["Apple", "Apple", "Windows", "none", "Windows", "Apple"]
.
The function should return three values: how many Apple computers there are,
how many
Windows computers there are, and how many people don't have a computer.
-
def countLaptops(laptops): numApples = 0 numWindows = 0 numNone = 0 for laptop in laptops: if laptop == "Apple": numApples = numApples+1 elif laptop == "Windows": numWindows = numWindows+1 elif laptop == "None": numNone = numNone+1 return numApples, numWindows, numNone
As a prelude to the next exercise, here's several more examples of using Python strings that you should type in.
-
"b" in "abcd"
-
"x" in "abcd"
-
"b" not in "abcd"
-
"x" not in "abcd"
-
import string string.letters
See also the Python documentation on string operations.
Some computer software is not able to handle people's names
that contain unexpected characters.
Define a function validateString
that takes two
arguments: a string representing a name, and a string that includes
all the characters to be considered valid. It returns a Boolean
indicating whether all the letters are valid.
Here, we show three possible solutions:
-
def validateString(name,validChars): for char in name: if char not in validChars: return False return True
-
def validateString(name,validChars): valid = True for char in name: valid = valid and (char in validChars) return valid
-
def validateString(name,validChars): valid = True for char in name: if char not in validChars: valid = False return valid
In statistics, a common misunderstanding is to believe that half of
a list of numbers are greater than their mean. Actually, that characterizes
the median. To illustrate the truth, define a function
fractionAboveMean
that takes a non-empty list of numbers and
returns the fraction that are above the list's mean.
For example, fractionAboveMean([3,8,1,9,2])
should return 0.4.
Of course, you should use the function arithmeticMean
from your first assignment.
-
def percentAboveMean(numbers): count = 0 mean = arithmeticMean(numbers) for number in numbers: if number > mean: count = count+1 return float(count) / len(numbers)
The previous class outlined how to quit generating population data when the data became too extreme. Make this change and verify that it works.