Why my code doesn't work?

Screen Link:

My Code:
countries['Pop Percent'] = countries['Population']/world_population*100
What I expected to happen:

Replace this line with the code output you expected to get

What actually happened:

Replace this line with the actual output/error

Hi @kevintran178

Can you give us more information about the issue? Remember to check the guidelines

With the info that you gave and reading what they are asking I assume that the mistake happens because you are using a third variable (world_population) that the autograder might not have in its system to evaluate. This is just speculation

Try to read again what they are asking you to do and if you can give us more info about the error it could help us to help you

2 Likes

From what I’m seeing, it’s probably a problem with world_population. Did you declare the variable before the calculation? If you could show us the error, it would help us to help you.

G.

2 Likes

Hi @kevintran178,

From trying your code, my hunch is it’s somehow related to comparisons between floats and probably some rounding differences depending on the order of operations.

Your code will pass the grader if you multiply by 100 early:

countries['Pop Percent'] = 100 * countries['Population']/world_population

But will not work if done later:

countries['Pop Percent'] = countries['Population']/world_population*100

The same behaviour can be seen with the solution.

The following will work:

countries['Pop Percent'] = 100 * countries['Population'] / countries['Population'].sum()

But this won’t work:

countries['Pop Percent'] = countries['Population'] / countries['Population'].sum() * 100

Basing floating-point numbers similarities solely on just the display can be misleading because if you try the following, you’ll see that both columns look deceptively similar:

countries['Pop Percent'] = countries['Population'] / world_population * 100
countries['Pop Percent 2'] = 100 * countries['Population']/world_population 

# print both
countries[['Pop Percent', 'Pop Percent 2']].head()

But they are not the same if you compare the two:

countries['Pop Percent'] = countries['Population'] / world_population * 100
countries['Pop Percent 2'] = 100 * countries['Population']/world_population 

different_pct = countries[countries['Pop Percent'] != countries['Pop Percent 2']]

different_pct[['Pop Percent', 'Pop Percent 2']]

68 countries have unequal pop percentage even though the display showed no difference.

In this case, my recommendation is to just multiply by 100 early to conform with the grader.

Also, consider sharing your experience and feedback with the Content & Product teams of Dataquest. Just click the ? button in the upper-right corner of any screen of the Dataquest learning platform, select Share Feedback, fill in the form, and send it.

Cheers.

1 Like