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 trialLuke Tate
Courses Plus Student 2,256 PointsWhy does my code return none, when appending a list within a Class?
class MuscleCar():
list = ["4 wheels", "big engine", "2 doors", "high-speed", "4 seat"]
"Class Attributes:"
hp = 300
spoiler = False
MuscleCar()
mCar = MuscleCar()
print(MuscleCar, mCar)
"Class Attributes:"
print(mCar.list)
print(mCar.list.append("exaust"))
mCar.list.extend([", exaust"])
print(mCar.hp)
print(MuscleCar.hp)
2 Answers
Jennifer Nordell
Treehouse TeacherHi there, Luke Tate! The short story is that you're trying to print
what is returned by the append method. But the append method just does something to the list and returns nothing. Much like a print statement itself prints something, but returns nothing. So if you did something like:
print(print("Hi"))
The output would be:
Hi
None
In the same way, you're appending something to the list and then printing out the result of running the append method. Instead, you'd want to update that list and then print out the list. Take a look:
class MuscleCar():
list = ["4 wheels", "big engine", "2 doors", "high-speed", "4 seat"]
"Class Attributes:"
hp = 300
spoiler = False
MuscleCar()
mCar = MuscleCar()
print(MuscleCar, mCar) # This prints the object
"Class Attributes:"
print(mCar.list) # This prints the the list stored in the class
print(mCar.list.append("exaust")) # this will print None because the append method returns None
mCar.list.extend([", exaust"])
print(mCar.list) # This is missing from your code. This prints the updated list
print(mCar.hp)
print(MuscleCar.hp)
Hope this helps!
Luke Tate
Courses Plus Student 2,256 PointsSo you're saying 1) I can't merely use methods within each other, and 2) to print an updated list via using the append, or extend method first then printing out the list again?
Luke Tate
Courses Plus Student 2,256 PointsThanks! I get it now!