Python For Loops- Screen 7/11

Screen Link:

My Code:
‘’'from csv import reader

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

#apps_data[1:] to exclude the first row with names
apps_data = apps_data[1:]

rating_sum = 0
for row in apps_data:
rating = row[7]
rating_sum = rating_sum + rating

print(rating_sum)

‘’’

What I expected to happen:

Replace this line with the code output you expected to get

What actually happened:

Replace this line with the actual output/error
2 Likes

Sorry, I’m really bad at reading things.

What do you mean by “then why don’t we take the column as a variable such as for column in apps_data ??”

Do you have a code example on how that would look like? Or is it in your image?

Hi @jorgefbalves, try to check the indenting of your code the error might be from there

If the same list of lists (apps_data), has lists that have quotation marks, how do we then get around that as python will read the data type as a string making it impossible to add rating_sum + rating?

1 Like

Hi @Keamogwerane,

Just like @OlutokiJohn explained in the following code:
quote=“jorgefbalves, post:1, topic:565007”]
rating_sum = 0
for row in apps_data:
rating = row[7]
rating_sum = rating_sum + rating
[/quote]

You can cast the quotations(string values) in row[7] to a float but you will need to start apps_data[1:] so you do not try to convert the headers in apps_data[0] to a float.

You could write something like:

rating_sum = 0
for row in apps_data[1:]:     
     rating = float(row[7])
     rating_sum += rating

I added a short explanation of each line of code for your review:

rating_sum = 0

Create a variable to store the sum of the rating column

for row in apps_data

for row in apps_data[1:]:
Create for loop to loop through all the rows in apps_data.
You want to start at index 1 so you can exclude the header row at index 0 of apps_data
Remember you are casting the row value to a float and if you try to cast the header at index 0 of apps_data to a float - you will get an error because you cannot cast a string to a float.

rating=float(row 7)

rating = float(row[7])
Here you are casting the string value in row[7] to a float so you can add to the rating_sum variable

rating_sum += rating

rating_sum += rating
Here, you are adding the rating value to the rating_sum variable.
You can also write this as :
rating_sum = rating_sum + rating

2 Likes

Assuming that the question is for the question is for this screen, the code above will iterate over the elements in apps_data[1] only, and not iterate row by row from apps_data[1] until the last row.

It will cause an error because it’ll try to index every element in apps_data[1] and those elements are not necessarily indexable or their length is less than 7. Also, the float casting will fail if it receives a string like “Facebook” which can’t be casted into a float number.

I believe what you meant is the following (slight modification):

rating_sum = 0
for row in apps_data[1:]:     
     rating = float(row[7])
     rating_sum += rating

Extra, seeing the errors when iterating over apps_data[1]:

for row in apps_data[1]:
    print(row, end=" - ")
    try:
       rating = float(row[7])    
        print("Everything's fine")
    except Exception as e:
        print(e)
2 Likes

@wanzulfikri
You are right- I left off the ‘:’
Thank you

1 Like