C2 W4 Decision Tree with Markdown - Information gain

I have a problem with compute_information_gain function. If I execute the function, the expected results are equals than my results, but the test are not passed.

(Solution code removed, as posting it publicly is against the honour code of this community, regardless if it is correct or not. You can share the errors you get)

Anyone else have the same problem or anyone can help me.

Thank you !

Hi @Maria_Novo ,

The way your code calculate the w_left and w_right is incorrect. Below is the instruction:

  • 𝑤left�left and 𝑤right�right are the proportion of examples at the left and right branch, respectively

So, it should be using the x_node based on the feature it is splitting on.

Thanks a lot @Kic ! Problem solved!

I had this same error and I don’t understand your explanation on how to fix it.

This is the error I got in W4 E3:

Anyone have any ideas? I’m very new to python3 coding and very very confused.

Hi @Amanda_Hamilton,

In short, the error message you shared means that, the code was calling a variable named “entropy” that was not defined before. You need to define the name before asking Python to use it.

For more, I can suggest a way for you to help yourself. Since you are new to Python, you are going to need it as your first aid.

To begin with, let’s be clear about one thing - the following two are both errors, but not the same error.


The first error was about getting a wrong result, whereas the second one is a syntax error. You won’t find any luck on Google with the first one, but for the second one, it won’t take more than a second for Google to get you some good read:

You see, the first step is to type the error message there, then google shows us the result, but there are 3 points I want to make:

  1. Stackoverflow is a very helpful and popular platform to find all sorts of problems and solutions previously discussed by all levels of users of tools like Python. However, one down side is, sometimes, the problems were too specific that only the persons in the discussion know what’s going on. So, I would suggest you to take a look, but not go too deep if you don’t feel familiar.

  2. Webpages like this second google result is more like a tutorial, so it is going to be more suitable for beginners. Google usually can show you more tutorial webpages like that, so I would suggest you to spend more time on those. The luck is you can find a webpage that match your style and be useful for a longer journey of your learning.

  3. My last point is, “entropy” is a variable name used in your assignment, so if your google result doesn’t look useful, try to remove such variable names because no one else on the internet except us knows this is our assignment. Even searching just “local variable referenced before assignment” can still give us very useful results.

Of course, google won’t tell you how to fix the error either, but you can learn and understand the error from those tutorials, and start trying to fix it yourself.


Hi Raymond,

I tried again from scratch and now I’m getting a new error and am equally confused. I went over the optional lab again and also followed all of the hints in the lab but I don’t understand where to input the needed information. My new error is as follows:

Hi @Amanda_Hamilton,

Try to google this → “python name is not defined”. Spend some time to learn about the error.

Note that I have added “python” to the list of searching keywords. It can be helpful.

I found the problem!! Thank you for your patience :slight_smile: