12.6. Practice with if

The program below 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. 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:

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

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.

Note

Discuss topics in this section with classmates.

Show Comments
You have attempted 1 of 7 activities on this page