Bird
0
0

Identify the syntax error in this polymorphic function:

medium📝 Debug Q6 of 15
Python - Polymorphism and Dynamic Behavior

Identify the syntax error in this polymorphic function:

def transform(value):
    if isinstance(value, list):
        return value + [1]
    elif isinstance(value, str):
        return value.upper()
    else
        return None

ACalling upper() on a list
BIncorrect use of isinstance for list
CUsing + operator on list and int
DMissing colon after else statement
Step-by-Step Solution
Solution:
  1. Step 1: Review the else statement syntax

    In Python, the else statement must end with a colon (:).
  2. Step 2: Check the given code

    The else line is written as else without a colon, causing a syntax error.
  3. Step 3: Verify other parts

    Using isinstance(value, list) and value + [1] is valid; value.upper() is valid for strings.
  4. Final Answer:

    Missing colon after else statement -> Option D
  5. Quick Check:

    Always add colon after else [OK]
Quick Trick: Check colons after else, if, elif statements [OK]
Common Mistakes:
  • Forgetting colon after else
  • Misusing operators on incompatible types
  • Calling string methods on non-string types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes