Blue Week Special Offer | Brighten your week!
days
hours
minutes
seconds

Conditional statements, operators, code is wrong meanwhile its matching the answer

Screen Link:
https://app.dataquest.io/m/313/conditional-statements/8/comparison-operators

My Code:

opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

apps_ratings=list()
for lis in apps_data[1:0]:
    rating = float(lis[7])
    price = float(lis[4])
    if price > 9:
        apps_ratings.append(rating)

avg_rating=sum(apps_ratings)/len(apps_ratings)
print(avg_rating)

n_apps_more_9=len(apps_ratings)
print(n_apps_more_9)

n_apps_less_9=len(apps_data[1:])-n_apps_more_9
print(n_apps_less_9)

What I expected to happen: i expected it`s the same as the answer

What actually happened:

ZeroDivisionErrorTraceback (most recent call last)
<ipython-input-1-e888577cfa31> in <module>()
     11         apps_ratings.append(rating)
     12 
---> 13 avg_rating=sum(apps_ratings)/len(apps_ratings)
     14 print(avg_rating)
     15 

ZeroDivisionError: division by zero

i suspect the the .csv file is not complete and have only the first few rows with 0.0 price value

Hey!
I think the problem is in the 6th line of your code.This line should be:
for lis in apps_data[1:]:
By this code, you can iterate through the rows of the data excepting the header row.I hope this’ll work now!

1 Like

Thanks a lot, i read my code many times and tried to debug it but missed the 0

1 Like

the way DataQuest help solve was :slight_smile:

if price > 9:
ratings.append(rating)

avg_rating = sum(ratings) / len(ratings)
n_apps_more_9 = len(ratings)
n_apps_less_9 = len(apps_data[1:]) - len(ratings)

and my way:

m_apps_less_9=
m_apps_more_9=
for row in apps_data[1:] :
rating = float(row[7])
price = float(row[4])
if price > 9 :
m_apps_more_9.append(rating)
if price <= 9 :
m_apps_less_9.append(rating)

avg_rating = sum(m_apps_more_9)/len(m_apps_more_9)
n_apps_more_9=len(m_apps_more_9)
n_apps_less_9=len(m_apps_less_9)