I'm new to Python and Django, and I modified this code from a tutorial. I'm getting TypeError: count() takes exactly one argument (0 given)
when I load the page. I've been troubleshooting and googling and can't seem to figure it out. What am I doing wrong?
def report(request):
flashcard_list = []
for flashcard in Flashcard.objects.all():
flashcard_dict = {}
flashcard_dict['list_object'] = flashcard_list
flashcard_dict['words_count'] = flashcard_list.count()
flashcard_dict['words_known'] = flashcard_list.filter(known=Yes).count()
flashcard_dict['percent_known'] = int(float(flashcard_dict['words_known']) / flashcard_dict['words_count'] * 100)
flashcard_list.append(flashcard_dict)
return render_to_response('report.html', { 'flashcard_list': flashcard_list })
The count in Python method returns a number as a return value. The integer value is the return value. When the count in Python returns 0, then it means that the value is not found in the list or string.
count() method is used to return the number of times an item appears in a list.
The most straightforward way to get the number of elements in a list is to use the Python built-in function len() . As the name function suggests, len() returns the length of the list, regardless of the types of elements in it.
count
requires an argument. It returns the number of instances of a particular item in a list.
>>> l = range(10) + range(10)
>>> l.count(5)
2
2
here is the number of 5
s in the list. If you want the length of a list, use len
.
>>> len(l)
20
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With