0

My code is giving me a name not defined error:

Traceback (most recent call last): File "split.py", line 22, in print(even)
NameError: name 'even' is not defined

Here's the instructions, and what I have written.. if someone could point me in the right direction it would be appreciated.

instructions: We are passing in a list of numbers. You need to create 2 new lists in your chart, then:

  • put all odd numbers in one list
  • put all even numbers in the other list
  • output the odd list first, the even list second

Tip: you should use the modulo operator to decide whether the number is odd or even. We provided a function for you to call that does this.

Don’t forget to define the 2 new lists before you start adding elements to them.

# Get our input from the command line
import sys
numbers = sys.argv[1].split(',')
for i in range(0,len(numbers)):
  numbers[i]= int(numbers[i])

def isEven(n) :
  return ((n % 2) == 0)

def Split(numbers):
  even = [ ] 
  odd = [ ] 
  for i in numbers:
    if i == isEven:
      even.append(i)
    else: 
      odd.append(i)

print(even)  # <= error here: NameError: name 'even' is not defined
print(odd)
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • 2
    Assume we don't want to run your code. Give us the full exact error it's giving you when you run it. The error will give you the line number. Identify the line of code for us, and explain why the error doesn't make sense to you. See how to create a [mcve]. – Peter Wood Feb 02 '19 at 19:53
  • thank you for the reply, sorry about being incomplete, i am very inexperienced and appreciate the help. The error i get is as follows: – Mason Carpenter Feb 02 '19 at 19:55
  • Traceback (most recent call last): File "split.py", line 22, in print(even) NameError: name 'even' is not defined – Mason Carpenter Feb 02 '19 at 19:55
  • 1
    This is true, `even` is not defined. There is a name `even` defined in the `Split` function but that doesn't leak out and become available everywhere... that would be chaos. – Peter Wood Feb 02 '19 at 19:56
  • Debugging 101 (which is so so basic): just print some variables, for example `numbers`, `even`, `odd`, ... – DisappointedByUnaccountableMod Feb 02 '19 at 19:56
  • i dont understand why it is not defined, am i not defining it when i split and tell it to use the isEven function? basically, i know im missing something, but i dont know what.. – Mason Carpenter Feb 02 '19 at 19:57
  • The `print` calls are not part of the `Split` function, so `even` and `odd` are not defined. – Peter Wood Feb 02 '19 at 19:57
  • Here’s a useful convention for your variable names - if it’s a list, make the variable name a plural - add an ‘s’ : so `evens`, `odds`, ... – DisappointedByUnaccountableMod Feb 02 '19 at 19:59
  • When you call the `isEven` function you need to pass it `i` as a parameter: `if isEven(i):` – Peter Wood Feb 02 '19 at 19:59

2 Answers2

3

First of all, welcome to Python!

There are multiple problems with your code.

First, in Split(), you run if i == isEven:. This checks whether i is the same as isEven, which it is not. isEven is a function, and i is an integer, so they can never be the same. Since you're trying to check whether i is even, you have to pass i into isEven(). Then, isEven outputs a boolean, saying whether the number is even or not:

if isEven(i):

That checks whether i is even.

Secondly, variables have something called scope. Scope is where the variable is defined and where it can be accessed from. Because you define even and odd in Split(), they can only be accessed from the code inside the function. That's why you can append things to it inside of Split(), but not print() it at the end. To fix this, you have to return the variables even and odd.

As well, functions have to be called first before the code inside of them is run. Right now, the function Split() is defined, but never ran, so even and odd can't exist, even if you return them.

Just like isEven() returned a boolean that said whether a number was even or odd, and you were able to access it in Split(), you can return the two lists from it, allowing you to access them and print them. To do this, add this at the end:

return even, odd

Now, whenever you call Split(), it will return a tuple of the two lists, that you can then access the individual elements and print:

output = Split(numbers) # Get the output from Split()
even = output[0] # Get the first element of the output, the evens
odd = output[1] # Get the second element of the output, the odds
print(even) # Print even
print(odd) # Print odd

If you still don't fully understand why this would work, and why your current code returns an error, I'd advise you to ask your teacher about it, as they can explain it the best to you.

1

You declare even and odd in the function's local scope. try returning the two lists in a tuple or something.

def Split(numbers):
  even = [ ] 
  odd = [ ] 
  for i in numbers:
    if i == isEven:
      even.append(i)
    else: 
      odd.append(i)
  rerturn (odd, even)

This is not the very code you should be using, but I hope you can figure it out from here :)

Sz. Zsolt
  • 47
  • 5