The problem: return total is not indented and sits outside process_payments. Python would raise an IndentationError or, if at module level, cause a syntax error for a bare return outside a function.
def process_payments(items):
total = 0
for i in items:
tax = i['price'] * 0.1
total = total + i['price'] + tax
time.sleep(0.1) # Simulate slow network call
return total # <-- BUG: outside the function!
Fix: Indent the return total by 3 spaces to align with for i in items::
def process_payments(items):
total = 0
for i in items:
tax = i['price'] * 0.1
total = total + i['price'] + tax
time.sleep(0.1) # Simulate slow network call
return total # <-- FIXED: indented inside the function
run_batch() has multiple indentation bugs
def run_batch():
users = [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
items = [{'price': 10}, {'price': 20}, {'price': 100}]
u = get_user_data(users, 3) # <-- BUG: outside function
print("User found: " + u['name']) # <-- BUG: print has indent but line above doesn't
The problems:
u = get_user_data(users, 3) is at zero indent — outside run_batch(). This will execute at import/module level before the function is even called.
The print("User found: ...") line is indented but its predecessor is not, creating an inconsistent indentation block. Python will raise an IndentationError: unindent does not match any outer indentation level.
Fix: Align all statements inside run_batch() consistently:
def run_batch():
users = [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
items = [{'price': 10}, {'price': 20}, {'price': 100}]
u = get_user_data(users, 3)
print("User found: " + u['name'])
print("Total: " + str(process_payments(items)))
- process_payments call is outside run_batch()
From the original code:
print("Total: " + str(process_payments(items)))
This line is also at module scope (no indent referencing run_batch), which means:
It accesses items which is defined inside run_batch() — NameError at runtime.
Combined with the indentation issues above, the code won't execute as intended.
-
process_payments calculates tax on every iteration incorrectly
python
for i in items:
tax = i['price'] * 0.1
total = total + i['price'] + tax
This adds the price + tax for each item and accumulates, which is functionally correct but could be simplified. Not really a bug, but the real issue is that time.sleep(0.1) is inside the loop, meaning processing 3 items takes ~300ms — a performance anti-pattern worth flagging in a code review.
The problem: return total is not indented and sits outside process_payments. Python would raise an IndentationError or, if at module level, cause a syntax error for a bare return outside a function.
def process_payments(items):
total = 0
for i in items:
tax = i['price'] * 0.1
total = total + i['price'] + tax
time.sleep(0.1) # Simulate slow network call
return total # <-- BUG: outside the function!
Fix: Indent the return total by 3 spaces to align with for i in items::
def process_payments(items):
total = 0
for i in items:
tax = i['price'] * 0.1
total = total + i['price'] + tax
time.sleep(0.1) # Simulate slow network call
return total # <-- FIXED: indented inside the function
run_batch() has multiple indentation bugs
def run_batch():
users = [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
items = [{'price': 10}, {'price': 20}, {'price': 100}]
u = get_user_data(users, 3) # <-- BUG: outside function
print("User found: " + u['name']) # <-- BUG: print has indent but line above doesn't
The problems:
u = get_user_data(users, 3) is at zero indent — outside run_batch(). This will execute at import/module level before the function is even called.
The print("User found: ...") line is indented but its predecessor is not, creating an inconsistent indentation block. Python will raise an IndentationError: unindent does not match any outer indentation level.
Fix: Align all statements inside run_batch() consistently:
def run_batch():
users = [{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}]
items = [{'price': 10}, {'price': 20}, {'price': 100}]
u = get_user_data(users, 3)
print("User found: " + u['name'])
print("Total: " + str(process_payments(items)))
From the original code:
print("Total: " + str(process_payments(items)))
This line is also at module scope (no indent referencing run_batch), which means:
It accesses items which is defined inside run_batch() — NameError at runtime.
Combined with the indentation issues above, the code won't execute as intended.
process_payments calculates tax on every iteration incorrectly
python
for i in items:
tax = i['price'] * 0.1
total = total + i['price'] + tax
This adds the price + tax for each item and accumulates, which is functionally correct but could be simplified. Not really a bug, but the real issue is that time.sleep(0.1) is inside the loop, meaning processing 3 items takes ~300ms — a performance anti-pattern worth flagging in a code review.