Getting only 0 as a result for conditional statement

Hi,

in this quest:
https://app.dataquest.io/m/313/conditional-statements/8/comparison-operators

The avg rating of apps which cost more than 9 dollars shall be calculated. I tried it with this way:

opened_file = open(‘AppleStore.csv’)
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)
ratings=
n_apps_less_9=0
n_apps_more_9=0

for row in apps_data[1:0]:
price=float(row[4])
rating=float(row[7])

if price >9.0:
    ratings.append(rating)
elif price <=8.0:
    n_apps_less_9=n_apps_less_9+1

if len(ratings)==0:
avg_rating=0.0
elif len(ratings) !=0:
avg_rating=sum(n_apps_more_9_dollar)/len(n_apps_more_9_dollar)
print(avg_rating)
print(n_apps_more_9)
print(n_apps_less_9)

But my avg_rating is 0 instead of a correct result. I don’t see, where I did my mistake… Can someone explain it?

Hi Thomas, welcome to the community!

Here are some things to think about as you look over your code:

  1. In your loop, you have apps_data[1:0]. I’m not sure if you meant for the 0 to be there or if it was a typo, but the code in your loop won’t execute properly.
  2. What are the variables n_apps_less_9, n_apps_more_9 and n_apps_more_9_dollar used for in your code?

After looking over these things, if you’re still stuck, let me know!

On a separate note, when you’re copying and pasting code in the forums, you’ll want to put three backticks before and after your lines of code so that it formats properly. (See this post if it’s unclear.) Thanks!

1 Like

Hi,

thanks for the welcome & the tips.

The 0 was exactly the issue. Now it runs smoothly. Thanks a mil!

1 Like