Python Interview Questions : Logic to find the count of one specific 'value' in a dictionary

 


Question:
The aim of this challenge is, given a dictionary of people's online status, to count the number of people who are online.For example, consider the following dictionary:
statuses = {
    "Alice": "online",
    "Bob": "offline",
    "Eve": "online",
}
In this case, the number of people online is 2. Write a function named online_count that takes one parameter. The parameter is a dictionary that maps from strings of names to the string "online" or "offline", as seen above.

Code:
def online_count(statuses):
    online_count= 0
    for key in statuses:
          if statuses[key] ==  "online":

             online_count = online_count+1
    return online_count
statuses = {
    "Alice": "online",
    "Bob": "offline",
    "Eve": "online",
    "Ev2": "online",
}
online_count = online_count(statuses)
print(online_count)

Output:



Comments

Post a Comment