counting online people of a dictionary

I have a dictionary of people’s online status, and I want to count the number of people who are online. Example :

statuses = {
"Alice": "online",
"Bob": "offline",
"Eve": "online",
}

And I tried this code to do that

def online_count(PeopleDictionary):
    for person in PeopleDictionary.items():
        if person[1] == 'online':
            count = person.count('online')
            return count
statuses = {
    "Alice": "online",
    "Bob": "offline",
    "Eve": "online",
}
print(online_count(statuses))

But in the output, it gave me 1 instead of 2

I am stuck and I don’t know how to count the online people in that dictionary so please tell me what to do. Thanks in advance

Answer

You need to add to count each time through the loop, not just assign it.

person.count('online') will always be 1, unless the person’s name is also online.

Also you should return after the loop is done, not during the loop.

def online_count(PeopleDictionary):
    count = 0
    for status in PeopleDictionary.values():
        if status == 'online':
            count += 1
    return count

But this can be simplified using the sum() function:

def online_count(PeopleDictionary):
    return sum(status == 'online' for status in PeopleDictionary.values())

You don’t need to use items() if you don’t need the keys, use values() to get just the statuses.

You can also convert values() to a list and use count():

def online_count(PeopleDictionary):
    return list(PeopleDictionary.values()).count('online')