## 1. If Statements ##

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

free_apps_ratings = []
for row in apps_data[1:]:
    rating = float(row[7])
    # Complete the code from here
    price = float(row[4])
    if price == 0.0:
        free_apps_ratings.append(rating)
avg_rating_free = sum(free_apps_ratings) / len(free_apps_ratings)

n_free_apps = len(free_apps_ratings)
percentage_free_apps = (n_free_apps / len(apps_data[1:])) * 100
    

## 2. Booleans ##

a_price = 0
prices = [0, 2, 0, 0, 0]
app_and_price = [['Facebook', 0], ['Instagram', 0], ['Plants vs. Zombies', 0.99],
                 ['Minecraft: Pocket Edition', 6.99], ['Temple Run', 0],
                 ['Plague Inc.', 0.99]]

if a_price == 0:
    print('This is free.')
if a_price == 1:
    print('This is not free')

free = []

for f in prices:
    if f == 0:
        free.append(f)
        
n_free_prices = len(free)

free_apps = []
for c in app_and_price:
    name = c[0]
    price = float(c[1])
    if price == 0:
        free_apps.append(name)
        
print(free_apps)

## 3. The Average Rating of Non-free Apps ##

non_free_apps_ratings = []

for app in apps_data[1:]:
    rating = float(app[7])
    price = float(app[4])
    
    if price != 0.0:
        non_free_apps_ratings.append(rating)
        
avg_rating_non_free = sum(non_free_apps_ratings) / len(non_free_apps_ratings)



## 4. The Average Rating of Gaming Apps ##

non_games_ratings = []

for app in apps_data[1:]:
    rating = float(app[7])
    genre = app[11]
    
    if genre != "Games":
        non_games_ratings.append(rating)
        
avg_rating_non_games = sum(non_games_ratings) / len(non_games_ratings)

## 5. Multiple Conditions ##

free_games_ratings = []

for app in apps_data[1:]:
    rating = float(app[7])
    price = float(app[4])
    genre = app[11]
    
    if price == 0.0 and genre == "Games":
        free_games_ratings.append(rating)
    
avg_rating_free_games = sum(free_games_ratings) / len(free_games_ratings)


## 6. The or Operator ##

games_social_ratings = []

for app in apps_data[1:]:
    rating = float(app[7])
    genre = app[11]
    
    if genre == "Social Networking" or genre == "Games":
        games_social_ratings.append(rating)
        
avg_games_social = sum(games_social_ratings) / len(games_social_ratings)

    

## 7. Combining Logical Operators ##

free_games_social_ratings = []
nonfree_games_social_ratings = []
for row in apps_data[1:]: # apps_data is already saved from previous screens
    rating = float(row[7])
    genre = row[11]
    price = float(row[4])
    
    if (genre == 'Social Networking' or genre == 'Games') and price == 0:
        free_games_social_ratings.append(rating)

# Write your code below
    
    if (genre == "Social Networking" or genre == "Games") and price != 0:
        nonfree_games_social_ratings.append(rating)
        
avg_free = sum(free_games_social_ratings) / len(free_games_social_ratings)        
avg_non_free = sum(nonfree_games_social_ratings) / len(nonfree_games_social_ratings)


## 8. Comparison Operators ##

over_9 = []
under_9 = []

for app in apps_data[1:]:
    price = float(app[4])
    rating = float(app[7])
    
    if price > 9.0:
        over_9.append(rating)
    if price <= 9.0:
        under_9.append(rating)

avg_rating = sum(over_9) / len(over_9)

n_apps_more_9 = len(over_9)

n_apps_less_9 = len(under_9)
    

## 9. The else Clause ##

for app in apps_data[1:]:
    price = float(app[4])
    
    if price == 0.0:
        app.append('free')
    else:
        app.append('non-free')
    
apps_data[0].append('free_or_not')

print(apps_data[0])

## 10. The elif Clause ##

for app in apps_data[1:]:
    price = float(app[4])
    
    if price == 0.0:
        app.append('free')
    elif price > 0.0 and price < 20:
        app.append('affordable')
    elif price >= 20 and price < 50:
        app.append('expensive')
    elif price >= 50:
        app.append('very expensive')

apps_data[0].append('price_label')

print(apps_data[:5])
