Skip to content
Advertisement

filter even and odd values from a dictionary and add these even and odd values to lists and write them to a txt file

code:

JavaScript

error: name ‘odd’ is not defined

Is it not found because it is empty in the lists but I add even and odd number. I do not understand why you gave such an error.How can i fix error?

Advertisement

Answer

Variables have a scope. If they are in a method, the variables are gone when the method is finished. That’s why you return stuff.

At the place where you call the method, you can then assign the results of the method to new variables.

Since you return a tuple, you can have 2 variables at the assignment:

JavaScript

Or, if you like to have the same names again

JavaScript

Here’s the full code. Read it carefully and try to understand what it is all about with the names. Note that they are different inside and outside the method.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement