ERROR HELP NEEDED: section:dictionaries and frequency tables: filtering for intervals

CODE:

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file) 
no_of_user_ratings = []
for row in apps_data[1:]:
     
    no_of_user_ratings.append(int(row[5]))
print(max(no_of_user_ratings))
print(min(no_of_user_ratings))

user_ratings_freq = {'<= 600000':0,'600000-1200000':0,'1200000-1800000':0,'1800000-2400000':0,'> 2400000':0}

for row in apps_data[1:]:
    user_ratings = int(row[5])
    
    if user_ratings <= 600000:
        user_ratings_freq['<= 600000'] += 1
        
    elif 600000 < user_ratings <= 1200000:
        user_ratings_freq['600000 - 1200000'] += 1
        
    elif 1200000 < user_ratings <= 1800000:
        user_ratings_freq['1200000 - 1800000'] += 1
        
    elif 1800000 < user_ratings <= 2400000:
        user_ratings_freq['1800000 - 2400000'] += 1
        
    elif user_ratings > 2400000:
        user_ratings_freq['> 2400000'] += 1
print(user_ratings_freq)

ERROR:

KeyErrorTraceback (most recent call last)
<ipython-input-1-38eb15cd08bf> in <module>()
     25 
     26     elif 1800000 < user_ratings <= 2400000:
---> 27         user_ratings_freq['1800000 - 2400000'] += 1
     28 
     29     elif user_ratings > 2400000:

KeyError: '1800000 - 2400000'

I choose 5 intervals…max(no_of_user_ratings) gave me something around 29,74,000

why is there a key error here?..the key exists in the dictionary I created…help

Check if you have declare the key with the same string name.

If you want default value to be zero for integers, use defaultdict.

from collections import defaultdict
user_ratings_freq = defaultdict(int)

When defining user_ratings_freq you did not use spaces for keys
user_ratings_freq = {’<= 600000’:0,‘600000-1200000’:0,‘1200000-1800000’:0,‘1800000-2400000’:0,’> 2400000’:0}, when incrementing counts you used spaces user_ratings_freq['1800000 - 2400000'] += 1.
They are different strings and don’t match the key you defined

you’re right…thanks

Hi shibalik.sanyal, could you mark this question as solved so future students can find it if you think it answers your question?