Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHumza Malak
3,064 PointsAdd a _str_ method to the DreamCar class. In the method, return a string that states the dream car's make and model. The
I have tried to add the str to the method with no success, what have I done wrong?
class DreamCar:
def __init__(self, make, model):
self.make = make
self.model = model
# insert your code here
def car(String="")
1 Answer
Steven Parker
231,269 PointsThis code seems incomplete, but there's several visible issues:
- the "def" line is not indented correctly (it's one space off compared to the other one)
- the instructions ask you to create a "__str__" method but this method is named "car" instead
- all methods should have a "self" parameter
- the "String" parameter is not needed
- the method isn't finished and still needs a code body
Also, another student recently asked about this challenge and it seems to have a bug.
See my answer to that other question.
Humza Malak
3,064 PointsHumza Malak
3,064 PointsI've changed it to this, class DreamCar: def init(self, make, model): self.make = make self.model = model # insert your code here def str(self, make, model): print("My dream car is a" +self.make +self.model)
Still getting an error
Edit: tried this and still an error
class DreamCar: def init(self, make, model): self.make = make self.model = model # insert your code here def str(self): return "My dream car is a {} {}".format(self.make, self.model)
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThat should work, except for the bug. Follow the link I left you above to the other question and see my answer there.
I was only able to get it pass using an "f-string":
return f"My dream car is a {self.make} {self.model}."