Bird
Raised Fist0

Identify the bug in this payment validation code snippet:

medium📝 Analysis Q6 of Q15
LLD - Design — Parking Lot System
Identify the bug in this payment validation code snippet:
def validate_payment(amount):
    if amount < 0:
        return "Invalid amount"
    elif amount == 0:
        return "Amount cannot be zero"
    else:
        return "Valid amount"

print(validate_payment(0))
AThe code correctly handles zero amount
BNo bug; output will be 'Amount cannot be zero'
CThe function should accept zero as valid
DThe condition for zero amount should be before negative check
Step-by-Step Solution
Solution:
  1. Step 1: Review conditions order and logic

    The code checks if amount < 0, then if amount == 0, else valid. This order correctly handles zero.
  2. Step 2: Check output for input 0

    Input 0 triggers the second condition, returning "Amount cannot be zero" as expected.
  3. Final Answer:

    No bug; output will be 'Amount cannot be zero' -> Option B
  4. Quick Check:

    Zero check correctly placed and handled [OK]
Quick Trick: Check conditions order carefully for correctness [OK]
Common Mistakes:
MISTAKES
  • Thinking zero should be accepted
  • Assuming condition order causes bug
  • Ignoring elif logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes