For loop conditional example doesn't make sense to me

So DQ has an alternative way to find the number of app ratings above 4. Instead of appending, they are incrementing the ratings by 1 and I am not sure why they would do that, wouldn’t that add 1 to the total ratings?

n_of_apps = 0

for row in apps_data[1:]:
rating = float(row[7])
if rating >= 4.0:
n_of_apps = n_of_apps + 1

Hi @rfarivar74

Welcome to DQ and DQ Community! :smile:

Nope. DQ solution (alternate) is not suggesting to add 1 to the rating. It’s incrementing the count of the apps that have a rating of 4 or higher.

I am guessing the primary solution appends each app (with a 4 or 4+ rating) in a list and then measures the length of the list using len() method example: len(app_list_name).

In the alternate solution, we are deriving that length with the help of a counter variable, in this case, n_of_apps which is first initialized to 0. Once the loop is completed, the total of this variable will give you the no. of apps that satisfy the rating condition.

You can verify by comparing len(app_list_name) == n_of_apps. It should result True.

Let me know if this didn’t help and confused you further. Happy Learning!

1 Like

Thanks for the clarification! Yes, the primary solution uses len(), but I’m still having some trouble.

I guess the concept that is confusing me is when you say, “incrementing the count of the apps”. I’m having trouble wrapping my head around the mechanics of what happens to each row when n_of_apps=n_of_apps+1. Why does it count the apps, rather than just printing each rating above 4?

Thanks again!


Hi @rfarivar74

The main purpose of either of the solutions is to get an answer to this question:

How many apps have a rating of 4.0 or greater?

Just printing the result won’t make much sense in this scenario.

Nothing happens to each row per se. It’s just that we need to check if the ratings >= 4.0 condition is satisfied for each app or not. If yes, we increase the counter value by 1. Let’s say there are 100 apps in total, and 40 of them have either a 4.0 or 4.0+ rating.

Once the for loop is complete you will get the total no. of applications that have 4.0 or 4.0+ ratings. In essence, you can conclude that 40% of applications are rated that way. We are just trying to find out how many apps. That’s why we are counting and not just printing.

1 Like

Oh got it now! Thank you so much for the help!

1 Like

hey @rfarivar74

Glad I could help!

1 Like

Can you also apply the syntax shortcut:
n_of_apps +=1
instead of
n_of_apps = n_of_apps + 1