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.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

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.

You have attempted 1 of 7 activities on this page