In chapter 6, the example used to explain the use case of while loops is poorly chosen.
The situation described in the example involves a knapsack, which has a maximum weight of 100 that should not be exceeded; however, when running the code the final weight is 120.
While the example still serves its purpose of explaining what a while loop does, I think the unwanted outcome may be confusing to unexperienced readers. The issue could be fixed by changing the while loop's condition to:
while current_weight + item_weights[index] < maximum_weight:
...
However, this adds some additional complexity and redundant code, so I'm not sure if this can be an acceptable solution. Another approach would be, of course, to come up with a completely different example of using while loops.
In chapter 6, the example used to explain the use case of while loops is poorly chosen. The situation described in the example involves a knapsack, which has a maximum weight of 100 that should not be exceeded; however, when running the code the final weight is 120.
While the example still serves its purpose of explaining what a while loop does, I think the unwanted outcome may be confusing to unexperienced readers. The issue could be fixed by changing the while loop's condition to:
However, this adds some additional complexity and redundant code, so I'm not sure if this can be an acceptable solution. Another approach would be, of course, to come up with a completely different example of using while loops.