Bird
0
0

The following code is intended to print "Falsy" if val is falsy, but it always prints "Truthy". What is the error?

medium📝 Debug Q14 of 15
Python - Data Types as Values
The following code is intended to print "Falsy" if val is falsy, but it always prints "Truthy". What is the error?
val = 0
if val == False:
    print("Falsy")
else:
    print("Truthy")
AUsing '==' instead of 'is' for comparison
BThe variable 'val' should be converted to bool first
CComparing with False misses some falsy values
DNo error; the code works correctly
Step-by-Step Solution
Solution:
  1. Step 1: Understand comparison with False

    Using val == False only matches if val equals exactly False or behaves equal to it. But some falsy values like 0 compare equal to False, so this seems correct here.
  2. Step 2: Check why it prints "Truthy"

    Actually, 0 == False is True, so it should print "Falsy". If it prints "Truthy", likely the code is different or the question expects the explanation that comparing with False is not reliable for all falsy values like empty containers or None.
  3. Final Answer:

    Comparing with False misses some falsy values -> Option C
  4. Quick Check:

    Use 'if not val:' to catch all falsy [OK]
Quick Trick: Use 'if not val:' to catch all falsy values, not '== False' [OK]
Common Mistakes:
MISTAKES
  • Thinking '==' always works for falsy check
  • Using 'is' instead of '==' incorrectly
  • Not realizing empty containers are falsy but not equal to False

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes