When one failure hides another

A school shop requires its program to add a 6-dollar delivery charge to the sum of item prices, then apply a 20% surcharge to that combined amount. A student writes this Python 3 function:


Python
def order_total(prices):
    total = 0
    for i in range(len(prices) + 1):
        total += prices[i]
    return total * 1.20 + 6

print(order_total([8, 15, 22]))


Python list positions start at 0. The function len gives the number of items, and range(n) supplies integers from 0 through n - 1.


Find the first invalid list index reached by the original program, as an exact integer. A programmer then repairs only the loop bounds so that every item price is added exactly once, leaving the return statement unchanged. Find both the amount this repaired version returns and the amount required by the specification, in dollars to one decimal place.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Bug Types (Syntax, Runtime, Logic) Questions: