Guard Invariants with assert

Easy ⏱ 8 min 77% acceptance ★★★★★ 4.5
Write checkout(items, total) that asserts two invariants before returning the confirmation string 'charged <total>': the cart is non-empty, and total is positive. Each assert must carry a message. (Note for discussion: asserts vanish under python -O, so they guard developer errors, not user input.)

Examples

Example 1
Input
checkout(['pen'], 10)
Output
'charged 10'
Explanation

Both invariants hold.

Constraints

  • Two assert statements with messages.
  • Return the formatted string.

Topics

Exception Handlingassert

Companies

FlipkartMyntra

Hints

Hint 1

assert items, 'empty cart'.

Hint 2

assert total > 0 with its own message.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.