An internet service provider has three subscription packages for customers to choose from:
Write a program that calculates a customer's monthly bill. It must ask the user to enter the letter of the package they purchased (A, B, C) and the number of hours they used (the user does not have to enter a whole number). It should then display the total charges (formatted to look like a dollar amount).
If the user does not give you a valid subscription package (capitalization does not matter), tell them. If the number of hours they used is not positive, tell them.
Here is an example of what your program should look like when it runs (what the user types is in
Here is the start of the program:
while
loops are like if statements that repeat while the condition is True
. The while
loop looks like:
#statements to execute while condition is True
The while loop starts with the Python keyword "while
". What follows is the "condition" (a Boolean expression), then a colon. This can be called the while loop header. On the next line, you begin the body of the while loop. Each line of the body must be indented (a tab or four spaces is standard). The body consists of statements to execute while the condition is True. The first non-indented line ends the body. Here is an example:
When the program first encounters the while loop, it evaluates the condition. If the condition is True
, then the body of the while loop executes. At the end of the body, the program jumps back to the top of the while loop and evaluates the condition again. If it's still True
, then it executes the body again. This continues until the condition is false.
What happens if that last line in the while loop body wasn't there? In other words, what happens if the code were:
Why do you think we needed to set keep_going
to 'y'
before we started the while loop? In other words, what happens if the code were:
What happens if keep_going
were set to something else before the while loop started? For example:
What values are invalid in the program above? How should we handle them?
In the programs above, the user has to enter three values each time through the loop. Sentinel values are a convenient way to remove the "do you want to repeat?" question. Basically, a sentinel value is a special value that would normally be considered invalid, but can instead be used to indicate that the user wants to stop. For example, in the example above a negative sales amount should be considered invalid, but maybe we can say that -1 means the user wants to stop the loop:
Notice that the condition for the while loop changed, the sales amount prompt changed, and the prompt was moved around. Why do you think each change was made?
while
loops can be used for input validation. The program below asks the user for a number. It then computes the square root of that number and displays the result.
One problem with this program is that it crashes on negative numbers. Before while loops, the best we could do was detect the negative number and stop the program, i.e.:
Now that we've seen while loops, we can ask again instead of ending the program:
Infinite loops are loops that never stop, i.e. the condition is never False
. There are two common reasons for this:
A very simple example is:
We saw another example of this above.
A very simple example is:
Just like nested if statements, it is possible to nest loops inside of other loops. For each time through the outer loop, the inner loop loops completely:
Why do you think minutes
is set to zero inside the first while loop? Why isn't it:
A accumulator variable is simply a variable that gathers, collects, or accumulates values during execution of a loop. In the example below, it will accumulate numbers as a running total. However, accumulator variables are just like any other variable, but with a special purpose: accumulating values. In addition to numbers, accumulators are also often strings or lists (called arrays in other languages ... more on these soon).
Here's an example of an accumulator variable. It sums up all the non-zero numbers the user enters, then displays the average. Note the use of a sentinel value to stop the loop.
<< Previous Notes | Daily Schedule | Next Notes >> |