Python 3.4.3 NameError: name "" is not defined

3.4k views Asked by At

This is using Python 3.4.3. I am very aware that this is a very common issue but I am an extremely inexperienced programmer and on all the other answers people talk about things that I don't understand at all. This is very basic, just for some practice and yet it is constantly having this error, and despite searching through over and over, I can find no explanation. Here it is:

def choose ():
    choice = input("Choose one - +, -, / or *.")
    if (choice != '+' and choice != '-' and choice != '/' and choice != '*'):
         print ("Please give a valid answer.")
         pass
    if (choice == '+'):
        add()
    if (choice == '-'):
         subtract()
    if (choice == '/'):
         divide()
    if (choice == '*'):
        multiply()

main()

The functions appear later on. Whenever I run it, it says 'NameError: name 'choice' is not defined', which is very frustrating. I think that placing the code into this messed with the indents, so sorry if my indents are all messed up, they were fine. Thanks if you can help :) I need a very straightforward answer please, still very early on.

1

There are 1 answers

1
Daniel On

In Python indentation is important! Use 4 spaces per level:

def choose ():
    choice = input ("Choose one - +, -, / or *.")
    if (choice != '+' and choice != '-' and choice != '/' and choice != '*'):
        print ("Please give a valid answer.")
    if (choice == '+'):
        add ()
    if (choice == '-'):
        subtract ()
    if (choice == '/'):
        divide ()
    if (choice == '*'):
        multiply ()

main ()