4.Retrieving Multiple List Elements

https://app.dataquest.io/m/312/lists-and-for-loops/4/retrieving-multiple-list-elements

row_1 = [‘Facebook’, 0.0, ‘USD’, 2974676, 3.5]
row_2 = [‘Instagram’, 0.0, ‘USD’, 2161558, 4.5]
row_3 = [‘Clash of Clans’, 0.0, ‘USD’, 2130805, 4.5]
row_4 = [‘Temple Run’, 0.0, ‘USD’, 1724546, 4.5]
row_5 = [‘Pandora - Music & Radio’, 0.0, ‘USD’, 1126879, 4.0]
fb_rating_data = [row_1[0], row_1[3], row_1[4]]
insta_rating_data = [row_2[0], row_2[3], row_2[4]]
pandora_rating_data = [row_5[0], row_5[3], row_5[4]]
avg_rating = (fb_rating_data[2] + pandora_rating_data[2] + pandora_rating_data[2])/3

After running the code I get:
avg_ratingfloat (<class ‘float’>)
3.8333333333333335
I’m supposed to get 4 but it’s not working. why?

row_1 = [‘Facebook’, 0.0, ‘USD’, 2974676, 3.5]
row_2 = [‘Instagram’, 0.0, ‘USD’, 2161558, 4.5]
row_3 = [‘Clash of Clans’, 0.0, ‘USD’, 2130805, 4.5]
row_4 = [‘Temple Run’, 0.0, ‘USD’, 1724546, 4.5]
row_5 = [‘Pandora - Music & Radio’, 0.0, ‘USD’, 1126879, 4.0]

fb_rating_data= [row_1[0], row_1[3], row_1[-1]]
insta_rating_data= [row_2[0], row_2[3], row_2[-1]]
pandora_rating_data= [row_5[0], row_5[3], row_5[-1]]
avg_rating=(fb_rating_data[2]+insta_rating_data[2]+pandora_rating_data[2])/3
print(avg_rating)

Hey @Shani

try to cast avg_rating as int instead of float using either code:

avg_rating = int(avg_rating)
print(avg_rating)

OR

print(int(avg_rating)

1 Like

Hello Rucha,
I did get 4 afteh converting to int as explained below, but it asks me to only use float… please help ?

fb_rating_data = [row_1[0] , row_2[3] , row_3[-1]]
insta_rating_data = [row_2[0] , row_2[3] , row_3[-1]]
pandora_rating_data = [row_5[0] , row_5[3] , row_5[-1]]

avg_rating = (fb_rating_data[2] + insta_rating_data[2] + pandora_rating_data[2]) / 3
avg_rating = int(avg_rating)
print(avg_rating)

hi @gls89686.aa

Apologies, I just jumped on the conversion.

In @Shani 's code, she has repeated pandora_rating[2] twice, hence not getting correct result:

avg_rating = (fb_rating_data[2] + pandora_rating_data[2] + pandora_rating_data[2])/3

@kamrul303 has given the corrected answer so no need to use any conversion/ casting by int.

simply print avg_rating and it should work:

avg_rating=(fb_rating_data[2]+insta_rating_data[2]+pandora_rating_data[2])/3
print(avg_rating)

Thank you Rucha, it did work now as the issue was cuz of the code lines.
fb_rating_data has row 1, row 2 and row 3. It should only be the same row number

fb_rating_data = [row_1[0] , row_2[3] , row_3[-1]]
insta_rating_data = [row_2[0] , row_2[3] , row_3[-1]]

Thank you all for your replies.
I tried the int and all, but finally got it right by using round! It turned the 3.8333333333333335 to 4.0, which is what was needed to pass this question.

hey @Shani

hope you also double checked on pandora_rating_data being used twice in your code.

avg_rating = (fb_rating_data[2] + pandora_rating_data[2] + pandora_rating_data[2])/3

this exercise doesn’t involve the use of round or int functions. I jumped on the conversion earlier, since I was using at that time for some other task.

hey @Rucha could you please explain why you have [2] next to the apps in calculating the avg_rating.

thanks

hey @phyladiamah

[2] indicates the index position of the rating element in the fb_rating_data list.

to access a list element we need to specify the index number of that element.

3 Likes