I don’t understand why 1 was added to n_of_apps. Though I know the condition is greater than or equal to 4 but how does this connect with 1 being added…??
Here we have to find out how many apps have a rating of 4.0 or more.
First, we initialized a variable
n_of_apps with a value of
for loop we are checking whether the rating is
4.0 or more, if it’s more then we are incrementing the value of
1. To increment, we add
1 to the previous value of
n_of_apps, whenever the if condition evaluates to
Hope it’s clear.
If you feel that your question was successfully solved, could you please mark the helpful answer as Solution? You can find here how to do it technically (in the GUIDELINE #5). Please consider this practice also for any other questions you might ask in the Community. This would be of great help both to your helpers and the other learners who can have the same questions in the future.
Many thanks and happy learning!
NOTE: This is an automated message.
Why do we increment the value by 1 when the rating is more than 4? I mean what’s the logic behind it??
We are using
n_of_apps to keep track of the count of apps having a rating of 4 or greater than 4 (because here we want to count only the apps with a high rating, starting from 4). Initially we have set it to
0. Whenever we find an app with a rating of 4 or more, we will increase the value of
n_of_apps by 1.
Let’s consider the first 5 rows and check the data
apps_data = apps_data[:5] print(apps_data)
[ ['id', 'track_name', 'size_bytes', 'currency', 'price', 'rating_count_tot', 'rating_count_ver', 'user_rating', 'user_rating_ver', 'ver', 'cont_rating', 'prime_genre', 'sup_devices.num', 'ipadSc_urls.num', 'lang.num', 'vpp_lic'], ['284882215', 'Facebook', '389879808', 'USD', '0.0', '2974676', '212', '3.5', '3.5', '95.0', '4+', 'Social Networking', '37', '1', '29', '1'], ['389801252', 'Instagram', '113954816', 'USD', '0.0', '2161558', '1289', '4.5', '4.0', '10.23', '12+', 'Photo & Video', '37', '0', '29', '1'], ['529479190', 'Clash of Clans', '116476928', 'USD', '0.0', '2130805', '579', '4.5', '4.5', '9.24.12', '9+', 'Games', '38', '5', '18', '1'], ['420009108', 'Temple Run', '65921024', 'USD', '0.0', '1724546', '3842', '4.5', '4.0', '1.6.2', '9+', 'Games', '40', '5', '1', '1'] ]
With the following code you could see that, when we come across a rating of 4.0 or more and we increase the value of
n_of_apps = 0 for row in apps_data[1:]: rating = float(row) if rating >= 4.0: n_of_apps = n_of_apps + 1 print('Rating is:', rating, 'and n_of_apps is:',n_of_apps)
Rating is: 3.5 and n_of_apps is: 0 Rating is: 4.5 and n_of_apps is: 1 Rating is: 4.5 and n_of_apps is: 2 Rating is: 4.5 and n_of_apps is: 3
Let know if you have any doubts.
Hi @mursuljamil ,
It seems like the exercise asks you to count the number of times the rating in row is 4.0 or greater The count is stored in variable n_of_apps
n_of_apps = 0
‘’’ n_of_apps variable starts at 0 and increases to 1 after each loop, if the rating is > =4
for row in apps_data:
rating = float(row) # let’s pretend the rating is 4.5
if rating >= 4.0: # triggers if statement
n_of_apps = n_of_apps + 1 # adds 1 to n_of_apps variable outside for loop
If you did not add the +1 you would overwrite the balance to 0 versus adding +1 to increase the n_of_apps variable by 1. For example, let’s pretend the rating is 4.6 in the 2nd for loop iteration. The “if statement” is triggered and +1 is added to n_of_apps variable. The n_of_apps variable is now 2. If you did not add n_of_apps +1 the n_of_apps variable outside the loop would reset to 0 again instead of adding 1. Each loop adds +1 to the n_of_apps outside the for loop.
You can also write as n_of_apps += 1 which means the same thing as n_of_apps = n_of_apps + 1