# If: Comparsion Question Got Code Right But feel like I overcomplated it

I’m on this section:

https://app.dataquest.io/m/313/conditional-statements/8/comparison-operators

I got the answer but when I compared my answer to the one dataquest has I feel like I went the wrong path and made things more complicated. Can somebody critique my code compared to the one that is right?

Mine:>

opened_file = open(‘AppleStore.csv’)

avg_rating_price_above_9 =
apps_less_9 =

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

``````if price > 9:
avg_rating_price_above_9.append(rating)
if price <= 9:
apps_less_9.append(rating)
``````

avg_rating = sum(avg_rating_price_above_9) / len(avg_rating_price_above_9)

print(avg_rating)
n_apps_more_9 = len(avg_rating_price_above_9)
print(n_apps_more_9)
n_apps_less_9 = len(apps_less_9)
print(n_apps_less_9)

the “correct” code:

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

``````if price > 9:
ratings.append(rating)
``````

avg_rating = sum(ratings) / len(ratings)
n_apps_more_9 = len(ratings)
n_apps_less_9 = len(apps_data[1:]) - len(ratings)

1 Like

Hi @scchoi31. I think you did great here. You successfully learned how to separate data by a specific criteria using loops and conditional statements. That’s the most important thing!

The main difference between your solution and the given solution boils down to focusing on the questions. Since the instructions only asked for the average of the apps greater than \$9, and not the average of the apps less than \$9, we don’t really need 2 separate lists. The given solution took advantage of this and only wrote enough code to put all the ratings of apps greater than \$9 into a list. Then to find out how many apps are less than \$9, they used subtraction. The 2nd list of apps less than 9 wasn’t necessary to the solution, so that’s why the code is shorter. Does that make sense?

When I went through this lesson for the first time, I also didn’t realize I could just subtract to find the number of apps less than \$9. It never occurred to me! Instead I used the loop to count them, which made my code longer than the given solution, too. I’m glad I did this though, because it’s something I’ve had to do many times in other projects!

So keep practicing, keep asking questions, keep comparing and growing. You’ll get better and have more confidence!

3 Likes

While I have your attention for this section can you explain this part of the code:

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)

This was in the 10. Elif clause

I entered my code with the

• apps_data[0].append(‘price_label’)

section and it still came out fine. Not sure what this part of the code is used for?

1 Like

Think of the data as a table with rows and columns. The first row of the data (`apps_data[0]`) contains the headers for the columns. The loop went through all the other rows and added something to each row based on the price. In a table, this looks like making a new column with values. The line `apps_data[0].append('price_label')` is adding a header to that column so that we know what that column represents later.

OH i see. It just didn’t show directly when I ran my code. I’m still getting use to the append syntax even though I do get the purpose of it as adding an element to my list

I’m assuming these are all very basic to pythons. How important does it play later? Especially if I’m trying to use it to work with data.

These intro to Python courses mostly focus on what you’ll need for data analysis and data science. At the end of this first course you’ll get the opportunity to put all of these things into practice with a guided project to do some practical data analysis. Those are pretty different because you’ll don’t have error-checking as you go, but you end up with something that you can share with others.

1 Like

Why the subtraction though? Haven’t been able to figure it out

It’s because we’re separating all the data into 2 categories with no overlap:

1. apps priced > \$9
2. apps priced <= \$9

Let’s say for example that there are 100 total apps. If we learn that 12 of them are more than \$9, then that means all the rest of them (100-12=88) are going to be less than \$9.

I understand now. Thank you for taking the time to explain