Bird
0
0

Why does this code cause an error?

medium📝 Debug Q7 of 15
Python - Constructors and Object Initialization
Why does this code cause an error?
class Animal:
    def __init__(self, species):
        species = species

a = Animal('Cat')
print(a.species)
Aprint statement syntax error
BConstructor missing self parameter
Cspecies is not assigned to self.species
DClass name should be lowercase
Step-by-Step Solution
Solution:
  1. Step 1: Check attribute assignment

    species = species assigns parameter to itself, not to object.
  2. Step 2: Understand attribute access

    self.species is not set, so print(a.species) causes error.
  3. Final Answer:

    species is not assigned to self.species -> Option C
  4. Quick Check:

    Assign to self.attribute to store value [OK]
Quick Trick: Assign parameters to self.attribute to save them [OK]
Common Mistakes:
  • Assigning parameter to itself instead of self.attribute
  • Missing self parameter
  • Incorrect print syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes