Name isn’t found in my Python application – Education Career Blog

keepProgramRunning = True

while keepProgramRunning:
    print "Welcome to the Calculator!"
    print "Please choose what you'd like to do:"

    print "0: Addition"
    print "1: Subtraction"
    print "2: Multiplication"
    print "3: Division"    


    #Capture the menu choice.
    choice = raw_input()

    #Capture the numbers you want to work with.
    numberA = raw_input("Enter your first number: ")
    numberB = raw_input("Enter your second number: ")

    if choice == "0":
        print "Your result is:"
        print Addition(numberA, numberB)
    elif choice == "1":
        print "Your result is:"
        print Subtraction(numberA, numberB)
    elif choice == "2":
        print "Your result is:"
        print Multiplication(numberA, numberB)
    elif choice == "3":
        print "Your result is:"
        print Division(numberA, numberB)
    else:
        print "Please choose a valid option."



def Addition(a, b):
    return a + b

def Subtraction(a, b):
    return a - b

def Multiplication(a, b):
    return a * b

def Division(a, b):
    return a / b

Here’s the error:

Traceback (most recent call last):
  File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\Tutorials\src\tutorials.py", line 23, in <module>
    print Addition(numberA, numberB)
NameError: name 'Addition' is not defined

Thanks for the help!

Ps. I realize the loop will never end, I haven’t added the menu option yet. 😛

,

You need to define your functions before calling them.

When the interpreter reads the line where Addition() is called it hasn’t yet reached the line where Addition() will be defined. It therefore throws an Exception.

,

Reorder your code, so that the functions will be defined before they’re used:

def Addition(a, b):
    return a + b

def Subtraction(a, b):
    return a - b

def Multiplication(a, b):
    return a * b

def Division(a, b):
    return a / b

keepProgramRunning = True

while keepProgramRunning:
    print "Welcome to the Calculator!"
    print "Please choose what you'd like to do:"

    print "0: Addition"
    print "1: Subtraction"
    print "2: Multiplication"
    print "3: Division"    


    #Capture the menu choice.
    choice = raw_input()

    #Capture the numbers you want to work with.
    numberA = raw_input("Enter your first number: ")
    numberB = raw_input("Enter your second number: ")

    if choice == "0":
        print "Your result is:"
        print Addition(numberA, numberB)
    elif choice == "1":
        print "Your result is:"
        print Subtraction(numberA, numberB)
    elif choice == "2":
        print "Your result is:"
        print Multiplication(numberA, numberB)
    elif choice == "3":
        print "Your result is:"
        print Division(numberA, numberB)
    else:
        print "Please choose a valid option."

alternatively, you can use main() function to keep it above everything:

def main():
    keepProgramRunning = True

    while keepProgramRunning:
        print "Welcome to the Calculator!"
        print "Please choose what you'd like to do:"

        print "0: Addition"
        print "1: Subtraction"
        print "2: Multiplication"
        print "3: Division"    


        #Capture the menu choice.
        choice = raw_input()

        #Capture the numbers you want to work with.
        numberA = raw_input("Enter your first number: ")
        numberB = raw_input("Enter your second number: ")

        if choice == "0":
            print "Your result is:"
            print Addition(numberA, numberB)
        elif choice == "1":
            print "Your result is:"
            print Subtraction(numberA, numberB)
        elif choice == "2":
            print "Your result is:"
            print Multiplication(numberA, numberB)
        elif choice == "3":
            print "Your result is:"
            print Division(numberA, numberB)
        else:
            print "Please choose a valid option."

def Addition(a, b):
    return a + b

def Subtraction(a, b):
    return a - b

def Multiplication(a, b):
    return a * b

def Division(a, b):
    return a / b

if __name__ == '__main__':
    main()

,

You need to define your functions before calling them. Function definitions are executable statements in Python and because of your infinite loop, they don’t get a chance to get defined.

You should move the four definitions to above the loop and this error will disappear.

On a more stylistic note, you should structure your module in a way that’s importable rather than just runnable. The __name__ == "__main__" trick which Python programs use is the canonical way and this article by the founder of the language offers some insights into how to construct it properly.

,

For that to work, you need to have some definition of Addition available by the time execution needs it. One way is to put your addition definitions higher in the file.

Another way is just to use the operator directly:

# was: print Addition(numberA, numberB)
print numberA + numberB

A third way is to use the functions in the operator module:

import operator
# ...
print operator.add(numberA, numberB)

Leave a Comment