Adding a counter as dictionary value

Hey,
I’m doing the Hurricane Analysis Project in the Data Science course, one of the exercises is asking to count how often a location was affected by hurricanes and add them to a dictionary, with the location name as Key, and the counter as Value.
But I can’t seem to manage to get a working piece of code, for now I have the code mentioned below, suggestions welcome.

def count_affected(areas_affected):
count = 0
for location in areas_affected:
for place in areas_affected:
if place not in areas_affected_count:
count = 1
areas_affected_count.update({place:[count]})
for place in areas_affected_count:
count += 1
return count

Hey there! Indentation is rather import in Python, and so I can’t accurately run your code unless you post it formatted. You can format it by copy pasting it between two rows of three backticks.

```
your code here
```
Which displays as

your code here

And preserves indentation.

Also for debugging could you share a link to the lesson, and perhaps some info on what makes your code “not working”? Any errors? Something seems off? The more info you give the better people can help :slightly_smiling_face:

"You believe that knowing how often each of the areas of the Atlantic are affected by these strong hurricanes is important for making preparations for future hurricanes.

Write a function that counts how often each area is listed as an affected area of a hurricane. Store and return the results in a dictionary where the keys are the affected areas and the values are counts of how many times the areas were affected.

Test your function on your hurricane dictionary."



def count_affected(areas_affected):
    count = 0
    for location in areas_affected:
     for place in areas_affected:
        if place not in areas_affected_count:
            count = 1
            areas_affected_count.update({place:[count]})
            for place in areas_affected_count:
                count += 1
                return count

print(areas_affected_count)```

It’s kinda difficult to debug for sure, without having the input and expected output, but one thing that seems a little off are you’re first two for loops. They essentially do the same thing, and location doesn’t seem to be used? Are they both needed?

What are you trying to do? It looks like you’re trying to grab the unique items out of areas_affected and count them. Though it looks like count_affected() is never called, so that could also be an issue.

Yes, essentially what I’m trying to do is to make a dictionary composed of all the unique places in Areas_Affected and count how often they occur.
the second loop I changed to “place in location”, it pertains lists, within lists

This is the list

                  ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'],
                  ['The Bahamas', 'Northeastern United States'],
                  ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda'],
                  ['The Bahamas', 'Cuba', 'Florida', 'Texas', 'Tamaulipas'], ['Jamaica', 'Yucatn Peninsula'],
                  ['The Bahamas', 'Florida', 'Georgia', 'The Carolinas', 'Virginia'],
                  ['Southeastern United States', 'Northeastern United States', 'Southwestern Quebec'],
                  ['Bermuda', 'New England', 'Atlantic Canada'], ['Lesser Antilles', 'Central America'],
                  ['Texas', 'Louisiana', 'Midwestern United States'], ['Central America'],
                  ['The Caribbean', 'Mexico', 'Texas'], ['Cuba', 'United States Gulf Coast'],
                  ['The Caribbean', 'Central America', 'Mexico', 'United States Gulf Coast'], ['Mexico'],
                  ['The Caribbean', 'United States East coast'],
                  ['The Caribbean', 'Yucatn Peninsula', 'Mexico', 'South Texas'],
                  ['Jamaica', 'Venezuela', 'Central America', 'Hispaniola', 'Mexico'],
                  ['The Caribbean', 'United States East Coast'], ['The Bahamas', 'Florida', 'United States Gulf Coast'],
                  ['Central America', 'Yucatn Peninsula', 'South Florida'],
                  ['Greater Antilles', 'Bahamas', 'Eastern United States', 'Ontario'],
                  ['The Caribbean', 'Venezuela', 'United States Gulf Coast'],
                  ['Windward Islands', 'Jamaica', 'Mexico', 'Texas'], ['Bahamas', 'United States Gulf Coast'],
                  ['Cuba', 'United States Gulf Coast'], ['Greater Antilles', 'Central America', 'Florida'],
                  ['The Caribbean', 'Central America'], ['Nicaragua', 'Honduras'],
                  ['Antilles', 'Venezuela', 'Colombia', 'United States East Coast', 'Atlantic Canada'],
                  ['Cape Verde', 'The Caribbean', 'British Virgin Islands', 'U.S. Virgin Islands', 'Cuba', 'Florida'],
                  ['Lesser Antilles', 'Virgin Islands', 'Puerto Rico', 'Dominican Republic',
                   'Turks and Caicos Islands'],
                  ['Central America', 'United States Gulf Coast (especially Florida Panhandle)']]```

You mentioned that you want your function to return a dictionary, but didn’t set that up inside the function.
You have to create the dictionary and that’s what would need to be returned at the end.
You updated a single number, count, and returned that, which is not what you wanted;
you should be updating things inside a dictionary instead.

And return should be outside all the loops.

Also, that innermost loop should just be an else instead of another loop,
because you want to add to the count if something is in the dictionary already.

spoiler:

def count_affected(areas_affected):
    areas_affected_count = dict() #empty dictionary
    for locations in areas_affected:
      for place in locations:
        if place not in areas_affected_count:
            areas_affected_count.update({place: 1}) 
        else:  #place in areas_affected_count:
            areas_affected_count[place] += 1
    return count

link to Hurricane Analysis project instructions

extra stuff

Alternatively, you could make a function that does the count for one place only:

def get_count_for(place): 
   count = 0
   for locations in areas_affected:
      if place in locations:
        # place can't be in locations more than once
        count += 1  
   return count

I think you meant:

    return areas_affected_count
1 Like

yes,
that should be
return areas_affected_count

1 Like

Ah yes, I see where I went wrong, if I run this code I’m getting:

<function count_affected at 0x000002364C553E20>
any thoughts on this?

It looks like you are returning the function name rather than the variable assigned to the accumulated value.

You are only printing the function itself. You want to print the value returned by the function when called. What needs to follow the function name in order to call the function?