0

I am writing a simple code to find even or odd numbers, the code was working just fine but maybe I did something wrong and it started giving me this error.

File "d:\Python\EvenOddFinder.py", line 12, in restart restartornot = input() File "", line 1, in NameError: name 'y' is not defined

    #Even or Odd Number Finder.
def start():
    userInput = input("Please input your number:")
    userInput = int(userInput)
    if userInput %2 == 0:
        print("The number " + str(userInput) + " is Even.")
    else:
        print("The number " + str(userInput) + " is Odd.")
    def restart():
        print("Do you want to restart?")
        print("Y/N")
        restartornot = input()
        if restartornot == "Y":
            start()
        elif restartornot == "y":
            start()
        elif restartornot == "N":
            exit()
        elif restartornot == "n":
            exit()
        else:
            print("Invalid Input.")
            restart()
    restart()
start()

Please help me I am quite new to Python.

Sumedh
  • 89
  • 1
  • 2
  • 8
  • 2
    Cannot reproduce. Your code works fine. Could you elaborate more on your problem? – Vasilis G. Jan 06 '19 at 14:27
  • It works fine till it gives me the answer but when it asks me to restart or not and if I enter "Y" it gives me the error. – Sumedh Jan 06 '19 at 14:51
  • 1
    Using python 3, it works fine for me too. If i answer "Y" it asks again as expected. Using python2, it gives the error you describe. – Valentino Jan 07 '19 at 21:33

1 Answers1

1

Assuming you are using Python 2 you should try using

restartornot = raw_input()
greg-bt
  • 71
  • 7
  • Actually I am using Python 3. According to it the code should work fine. – Sumedh Jan 07 '19 at 16:50
  • @Sumedh Are you *sure* you're using python 3? Because the error you described can't happen with the given code in python 3. I think maybe you have both python 2 and python 3 installed, and you're accidentally invoking it with python 2. – glibdud Jan 07 '19 at 21:32
  • 3
    @Dr.Greg `if restartornot == "Y" or "y":` is [not correct](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value). – glibdud Jan 07 '19 at 21:36