11.4. Using Multiple if statements¶
You can use more than one if
statement in your code. Here’s an example of a calculation
that uses two if
statements. Let’s compute the total cost of an item where the price
is based on the weight of the item. If the item weighs less than 1 pound then the price is
1.45 a pound. If the item weighs 1 pound or more the price is 1.15 a pound.
The program below is an attempt to do that. But it is broken in a subtle way.
For one value of weight
, the price
will not be set to any value, so the calculation
of total
will fail with an error that something
is not defined
. Edit the code and change the first line so that weight
has a different value.
Run it again and see what changes. Try it in the codelens as well to see how the different
values for weight changes the lines of code that are executed. Can you figure out the value
of weight
that will result in an error?
So how can we fix the bug? One way would be to change the second if to use >=
so that there
the program handles every situation correctly.
Another way we could solve the problem would be to set a price
as a default, to assume
that the weight if 1 or more. Then, we use an if
to change change it only if the weight
turns out to be less than 1.
- No, they're always the same.
- The end result is the same.
- Yes, they're different if the weight is exactly 1 pound.
- If the weight is exactly 1 pound the price will be 1.15 in both programs.
- Yes, they're different if the weight is under 1 pound.
- If the weight is under 1 pound the price will be 1.45 in both programs.
- Yes, they're different if the weight is over 1 pound.
- If the weight is over 1 pound the price will be 1.15 in both programs.
Modify the top program to use >=
in its second if. Then try both programs for weights
of less than 1, 1, and more than 1. Are there values for weight that make the two programs
above print different results when the same weight is used in both programs?
Check your understanding
The following program should calculate the total price, but the lines are mixed up. The price is based on the weight. Items that weigh less than 2 pounds should cost 1.5. Items that weigh more than 2 pounds should cost 1.3. Drag the blocks from the left and place them in the correct order on the right. Be sure to also indent correctly!
- x will always equal 0 after this code executes for any value of x
- If x was set to 1 originally, then it would still equal 1.
- if x is greater than 2, the value in x will be doubled after this code executes
- What happens in the original when x is greater than 2?
- if x is greater than 2, x will equal 0 after this code executes
- If x is greater than 2, it will be set to 0.
Which of the following is true about the code below?
1if (x > 2):
2 x = x * 2
3if (x > 4):
4 x = 0
5print(x)