Problem with exercise 13 filtering for intervals

Screen Link:

My Code: <<<!–opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

n_user_ratings =
for row in apps_data[1:]:
n_user_ratings.append(int(row[5]))

max_ratings = max(n_user_ratings)
#print(‘The maximum ratings’, n_user_ratings)
min_ratings = min(n_user_ratings)
#print(‘The minimum ratings’, n_user_ratings)

user_ratings_freq = {‘0 - 10000’:0,‘100000 - 100000’: 0, ‘100000 - 5000000’: 0, ‘50000000 - 10000000’: 0, ‘1000000+’: 0}
for row in apps_data[1:]:
user_ratings = (int(row[5]))

if user_ratings <= 10000:
    user_ratings_freq['0 - 10000'] += 1
    
elif 10000 < user_ratings <= 100000:
     user_ratings_freq['10000 - 100000'] += 1
    
elif 100000 < user_ratings <= 500000:
     user_ratings_freq['100000 - 500000'] += 1
    
elif 500000 < user_ratings <= 10000000: 
     user_ratings_freq['500000 - 1000000'] += 1
    
elif user_ratings > 10000000:
     user_ratings_freq['10000000 +'] += 1

print (user_ratings_freq)Enclose your code in 3 backticks to format properly–>

Replace this line with your code

What I expected to happen:Output the user ratings.

What actually happened: KeyError Traceback (most recent call last)
in
27
28 elif 500000 < user_ratings <= 10000000:
—> 29 user_ratings_freq[‘500000 - 1000000’] += 1
30
31 elif user_ratings > 10000000:

KeyError: ‘500000 - 1000000’

Replace this line with the output/error

In the future, please provide a link to the mission screen and provide a more descriptive title for your post. I’m not sure what “exercise 13” is or where to find it. :slightly_frowning_face: That said, thank you for providing such good output for your error and all of your code as this (I believe) has allowed me to help you…I hope! :smiley:

Whenever you see a KeyError when working with a dictionary, it means we should take a deeper look at what keys are in the dictionary and to also look at the lines of code that are trying to access the dictionary. In your case, according to the error we are given, it’s the key 500000 - 1000000 (500,000 - 1,000,000) that’s causing the error (line 29 in your code editor). So let’s look at the code where you define the dictionary and see what we can learn.

Let me just reformat the keys into a bulleted list so that’s easier to read:

  • 0 - 10,000
  • 100,000 - 100,000
  • 100,000 - 5,000,000
  • 50,000,000 - 10,000,000
  • 1,000,000+

Does anything jump out at you here? I see a couple things that could be causing your error…

Let me know if you’re still stuck after making changes to where you define your dictionary based on what we see above.