Learn how to identify and fix common mistakes in Python classes with this step-by-step guide. Perfect for beginners to understand constructors, instance variables, and methods in object-oriented programming.
Difficulty Levels: Beginner ✅
Topic: Classes
Below is a Python class with mistakes. Your task is to identify and fix the errors to make the code functional.
class Person:
def __init__(name, age):
name = name
age = age
def greet():
print("Hello, my name is " + self.name)
person1 = Person("Alice", 30)
person1.greet()
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name)
person1 = Person("Alice", 30)
person1.greet()