Checkpoint 23.4.1.
In the program above, what value for weight will result in an error complaining that price is not defined?
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.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 change the lines of code that are executed. Can you figure out the value of weight
that will result in an error?>=
so that there the program handles every situation correctly.price
as a default, to assume that the weight is under a pound. Then, we use an if
to change it only if the weight turns out to be 1 or more.>=
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?if (x > 2):
x = x * 2
if (x > 4):
x = 0
print(x)