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 trialMarcos López
7,493 Pointswhy is my code wrong?
If I get the array of Article objects, is just format the string output with the length of that array
from django.http import HttpResponse
from .models import Article
# Write your views here
def article_list(request):
articles = Article.objects.all()
output = "There are {0} articles".format(articles.len())
return HttpResponse(output)
1 Answer
Steven Parker
231,269 PointsLooks like you realized you needed len(array)
on your own.
I'm not aware of a language that uses a method for length. I know in JavaScript it's a property (array.length).
Ross Trimble
3,598 PointsC++ uses the length method for strings.
my_str.length()
returns the number of characters in a string. Most of the other standard containers in C++ (arrays, lists, vectors, also strings as well) use the size method my_array.size()
for the number of items inside them.
Marcos López
7,493 PointsMarcos López
7,493 PointsSorry, I put articles.len() and the right way is len(articles), maybe I was confused with other programming languages that is the way to get the len of an array