tags:

views:

64

answers:

4
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. :P

+8  A: 

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.

Tim Pietzcker
+5  A: 

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()
Lie Ryan
I keep forgetting python is interpreted and not compiled! Thanks for the help. :)
Serg
@Sergio Tapia: actually Python is compiled to bytecode (similar to Java/C#), and all the functions would already be compiled before the program starts, however the semantic of the compilation is late binding (unlike Java/C#), and functions objects are only assigned to a callable names when the def-statement is executed (in Java/C#, functions calls are resolved at compile time).
Lie Ryan
@Sergio: Even compiled languages (C at least) will complain if you use a function before you've declared it.
Nick T
C# doesn't have this problem, I can declare it on the bottom of the class if I like and call it from the top of the class. I think Java can do this as well.
Serg
@Sergio Tapia: yes, that is true for python's class (or C/C++'s class) as well, since by the time any method in the class is executed, the class will already be fully defined. In short, if you don't want to think about function declaration/definition order, put it in a class.
Lie Ryan
@Lie Ryan: Good to know, thank you!
Serg
+1  A: 

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)
ianmclaury
+1 for `operator`
Daenyth
updated to be less obtuse about where to define your functions
ianmclaury
+1  A: 

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.

Noufal Ibrahim