Practice with if¶
The program below is broken in a subtle way. For one value of
price will not be set to any value, so the calculation of
total will fail with an error that something
is not defined. This is why professional programmers will assign some value to a variable like
price at the beginning of the program, so that errors like this won’t happen. Can you figure out the value of
weight that will result in an error? Modify the code below to try different values for weight.
Try different values for
weight in the above code and then answer the question below:
It is certainly possible to have multiple
if statements, and each one can match (or not match) the data. Imagine a more complicated price scheme, where the price is based on the weight, but there is also a 10% discount for buying more then 10 items.