0

this is all of my code so far. the function works if I input x manually. I'm also just starting out so any general info on how to generally make it more pythonic would be apreciated

def iseven(x):
    if x % 2 == 0:
        return x," is even"
    else:
    return x," is odd"
it = (1)
while it >= 0:
    number = input("What number: ")
    print iseven(number)
    new_num = input("another number? answer (Y/N):")
    if new_num == "Y":
        it += 1
    else:
        it = 0

this is the error NameError: name 'Y' is not defined

2 Answers2

0

Just change the line:

new_num = input("another number? answer (Y/N):")

to:

new_num = raw_input("another number? answer (Y/N):")

You need to change: while it >= 0: to while it: as well so that you can be able to exit the loop if you select N on prompt.

dopstar
  • 1,478
  • 10
  • 20
  • Because the OP uses python2 in his example and Y/N means a character Y or N must be specified to continue or to terminate the program. `raw_input` does not try to interpret the input data while `input` do. – dopstar Oct 17 '15 at 21:53
0

You may want to avoid input in such cases where you're processing general input and prefer raw_input:

https://docs.python.org/2/library/functions.html#raw_input

This avoid the eval that's taking place that's causing your error - it's looking in the program's scope for something named "Y". If you type "it", here, for example, it will evaluate the code, which is generally unsafe (though fairly benign here I suppose).

https://docs.python.org/2/library/functions.html#eval

Will Hogan
  • 909
  • 4
  • 9