✅ The expected output of the code down below should be this:
2025 Toyota Camry
2025 Honda Civic
❌ However, the code outputs this instead:
return f"{year} {smake} {model}"
^^^^
NameError: name 'year' is not defined
What is the problem? Drop your answer in the comments.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer = 0 # Initialize odometer at 0
def get_description(self):
return f"{year} {smake} {model}"
# Creating instances of the Car class
car1 = Car("Toyota", "Camry", 2025)
car2 = Car("Honda", "Civic", 2025)
# Using the methods of the Car class
print(car1.get_description()) # Output: 2025 Toyota Camry
print(car2.get_description()) # Output: 2025 Toyota Camry
the return statement in get_description() needs to refer to the object's member variables by dereferencing the object using "self.<member-variable>". Note the minor typo "smake" instead of "make" as well
def get_description(self):
return f"{self.year} {self.make} {self.model}"
There should be a self.year on the instance method. Actually there should be self on the others as well. Btw im loving your daily python projects bought your course on udemy. Thanks for this free emails.