Bird
0
0

Find the error in this payment retry logic:

medium📝 Analysis Q7 of 15
LLD - Design — Parking Lot System
Find the error in this payment retry logic:
def retry_payment(attempts):
    while attempts > 0:
        print("Trying payment")
        attempts -= 1
    print("Failed")

retry_payment(2)
AThe print("Failed") should be inside the loop
BThe decrement of attempts is outside the loop causing infinite loop
CNo error; code runs correctly
DThe function should return a value
Step-by-Step Solution
Solution:
  1. Step 1: Analyze loop and decrement placement

    The decrement of attempts is inside the while loop, so attempts decreases each iteration.
  2. Step 2: Consequence of decrement placement

    With decrement inside loop, the loop terminates correctly after attempts reach zero, avoiding infinite loop.
  3. Final Answer:

    No error; code runs correctly -> Option C
  4. Quick Check:

    Decrement inside loop ensures loop termination [OK]
Quick Trick: Decrement inside loop avoids infinite loops [OK]
Common Mistakes:
MISTAKES
  • Placing decrement outside loop
  • Assuming print location causes error
  • Expecting function to return without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes