Python If, Else, Elif Statements: Multiple Conditions

[Screen Link: ]

(Learn data science with Python and R projects)

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

^ this is the answer. why is the ‘s’ in ratings not consistent?, and the code still works.

if i standardise and include ‘s’ for all ratings, the code is wrong.

thank you for the help. :slight_smile:

Hi @DebbieTan

ratings and rating are not the same.

The variable ratings is a list here, and the variable rating stores one float value at a given time.

Think of it like a train car/carriage. rating is one train car. Attaching multiple cars like this one after the other makes one long train (ratings).

You first extract the eight element from each row in the apps_data list via this code:

 rating = float(row[7])

# for example
1st row has a rating of ‘5.0’
2nd row has a rating of ‘4.5’
3rd row has a rating of ‘4.0’

Then append this element to the ratings list via this code:

ratings.append(rating)

You will end up with a list like this.

> print(ratings)
> ['5.0', '4.5', '4.0']
2 Likes

hello! ah okay i understand.

just to clarify-

  1. is python able to identify the extra ‘s’ as the code to make it one ‘long train’?
    meaning to say, if i were to use two different words
    e.g. long train (points), carriage (rating), would it be able to execute the same result?

@DebbieTan

Show me the whole code with points/ carriage etc. that you would write for me to answer that.

I have struggle with this part of lesson it gives me an Error which my answer isn’t according to value that they expect
I though if “row” work maybe “col” work too , and that was true but result didn’t change

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

games_social_ratings = []
games_social_ratings_rate=[]
for row in apps_data[1:]:
    rating = float(row[7])
    genre = row[11]
    # Complete code from here
    if genre == 'Social Networking' or 'Games':
        games_social_ratings.append(genre)
for row in apps_data[1:]:
    rating = float(row[7])
    genre = row[11]     
    if genre == 'Social Networking' or 'Games':
        games_social_ratings_rate.append(rating)
        rate_sum = sum(games_social_ratings_rate)

print(len(games_social_ratings))

C_games_social_ratings = []
for col in apps_data[1:]:
    C_rating = float(col[7])
    C_genre = col[11]
    if C_genre == 'Social Networking' or 'Games':
        C_games_social_ratings.append(C_rating)
        #C_rating = sum(C_rating)
        #print(sum(C_rating))
print (len(C_games_social_ratings))
print (rate_sum)
avg_games_social=sum(games_social_ratings_rate)/len(games_social_ratings)
print(avg_games_social)
#avg_games_social = sum(C_rating)/len(games_social_ratings)
#print(avg_games_social)"""

1 Like

I found my mistake and did it correct my mistake is in line `

I have to write it as `if genre == ‘Social Networking’ or genre==‘Games’ :
games_social_ratings.append(rating)``
thank you all i solved

2 Likes

I have a question with the answer for the following DQ instruction:

Complete the code in the editor to label each app as “free,” “affordable,” “expensive,” or “very expensive.” With the code:

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

for app in apps_data[1:]:
price = float(app[4])
# Complete code from here
if price == 0:
app.append(‘free’)
elif price > 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])

My question is: how does the app.append(‘free/affordable/expensive etc.’) line affect the apps_data variable?

the print at the end shows that apps_data is being appended to, but when using the append function I am referring to the variable “app”. Shouldn’t the code be apps_data.append(‘free/affordable/expensive/very expensive’)? If not, why does app.append make changes to apps_data? Thank you so much for the help! :slight_smile: