在我的Python应用程序中找不到名称

在我的Python应用程序中找不到名称

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


这是错误:

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


谢谢您的帮助!

附言我意识到循环将永远不会结束,我还没有添加菜单选项。 :P

最佳答案

重新排序代码,以便在使用函数之前先对其进行定义:

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."


或者,您可以使用main()函数将其保留在所有内容之上:

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()

关于python - 在我的Python应用程序中找不到名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3978787/

10-09 06:15