Bird
0
0

Identify the bug in this code:

medium📝 Debug Q7 of 15
Python - Data Types as Values
Identify the bug in this code:
items = [0, 1, 2]
for item in items:
    if item:
        print("Falsy")
    else:
        print("Truthy")
ANo bug, code works correctly
BThe loop should use 'if not item:' instead of 'if item:'
CThe list contains invalid values
DThe print statements are reversed for truthy and falsy
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the condition and prints

    The condition 'if item:' is True for non-zero values, but prints "Falsy" which is incorrect.
  2. Step 2: Correct the print statements

    When item is truthy, it should print "Truthy", and when falsy, print "Falsy".
  3. Final Answer:

    Print statements are reversed -> Option D
  4. Quick Check:

    Truthy check must print "Truthy" [OK]
Quick Trick: Match print messages to condition truthiness [OK]
Common Mistakes:
MISTAKES
  • Swapping truthy and falsy outputs
  • Misunderstanding 'if item:' condition
  • Assuming 0 is truthy

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes