Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: count() takes exactly one argument

Tags:

python

django

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 })  
like image 901
Alex Avatar asked Feb 25 '12 20:02

Alex


People also ask

What value is returned by count () If the argument is not found in the 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.

What is the list count?

count() method is used to return the number of times an item appears in a list.

How do you count elements in a list in Python?

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.


1 Answers

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 5s in the list. If you want the length of a list, use len.

>>> len(l)
20
like image 200
senderle Avatar answered Oct 27 '22 20:10

senderle