Challenge 2 Megathread

When you declare difference in your code, you’re saving the results of ((sum(city_price) - sum(country_price)) / sum(country_price)) inside of that variable.

Programmers tend to do that to make their code more readable, so when you print(difference) afterwards, it’s easier to read.

In more complicated programs, programmers will also tend to break their code into smaller functions, to make it easier to read and debug.


makes sense - thank you!

Can someone explain to me why this approach is mathematically correct ?

Wouldn’t taking the individual percentage differences between the items and then averaging the result be more accurate representation of the answer to the question? I feel like the approach taken in this solution is just not right.


My understanding is that, with the given grocery list, the total price paid in the city for groceries was 25.8% higher than what it would have been in the country.

The city prices and the country prices are reversed. If the country prices are higher, they should be the higher number. As someone who used to live in the country, these prices are backwards.

This should be corrected for the next go around.

1 Like

Thank you!

I did something on the same lines and it worked as well:
print ((sum(city_price) - sum(country_price))/sum(country_price))


They’re both mathematically the same - the difference between two sums is equal to the sum of the differences ( sum(A) - sum(B) === sum(A - B)).

Here’s how to calculate it both ways. diff_1 adds the items up and then takes the difference, diff_2 takes the difference between the price of each individual item and then adds those together. If you try it, you’ll see diff_1 == diff_2.

Spoilers below:

diff_1 = (sum(city_price) - sum(country_price)) / sum(country_price)
diff = []
for i in range(len(city_price)):
    diff.append(city_price[i] - country_price[i])

diff_2 = sum(diff)/sum(country_price)
print(diff_1, diff_2)

Love seeing that different folks did this different ways. I found I had to look at a website outside of the resources (links and introduction) provided. I didn’t find that the resources within the challenge were super helpful. As a newbie, it’s definitely already stressing me out to think that these are the “easy” challenges… :slight_smile:


This is exactly what I did.
I guess you would want to assign the output to “difference” or another variable so that you could then use that output later in your code, but I just did the math and looked at the output to find the right answer

Your method would give you the average price difference of a single product, whereas the question is concerned with the price difference of the grocery list as a whole. Neither method is more or less “mathematically correct” than the other, but they do not describe the same piece of information.

The coding/math on this is not complicated. The wording is way complicated.


it is not clear that it is related with totals.
array([ 44.54342984, 21.11650485, 28.3625731 , -38.62660944,
50.06257822, 20.01334223, 16.72240803, 60.24096386,
11.11111111, 33.55704698, 20.08032129, 135.1758794 ,
0. , 28.61230329, -10.28277635, 40.08016032,
76.92307692, 5.88235294, 30.46127067])

Much appreciated for breaking this down: am appreciating your videos on this. This one was also helpful for understanding running a build in order, and why you may get an error for an undefined term when you thought it was already defined.

Yes, the text is misleading. It implies that prices at the tiny country store would be higher, and so the question setter has reversed the labels for the lists of prices. The first day’s challenge was a mess, and this one is backwards (the math still works, but the text is badly worded). I’ll give them tomorrow to get one right, or I’m out.

It will give you correct answer even without the print function.

The question is alright this time, but the articles assigned not discus sum() function (which I know from SQL). They talk about using + , - , *, / etc. So I spent 20 minutes reading the articles and then did not use any of what was discussed there. There is also no mention of subtracting one list form the other (which I hoped could be done in Python without summing up each list first). If you need to sum the list up first and subtract next, what is the value of ordered lists?

Glad to make them, especially you are finding them valuable!

1 Like

even print is not needed
(sum(city_price) - sum(country_price))/sum(country_price)

1 Like

Yes, that’s true. someone just have to follow the hint.

No harm either way! The sums could potentially be used elsewhere, beyond the scope of the problem of course. Either approach is equally valid imo :slight_smile:

1 Like