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

时间:2010-10-20 14:13:31

标签: 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

4 个答案:

答案 0 :(得分:8)

您需要在调用之前定义函数。

当解释器读取调用Addition()的行时,它尚未到达将定义Addition()的行。因此它抛出异常。

答案 1 :(得分:4)

重新排序代码,以便在使用之前定义函数:

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

答案 2 :(得分:1)

要使其正常工作,您需要在执行需要时提供一些可用的添加定义。一种方法是将附加定义放在文件中更高的位置。

另一种方法是直接使用运算符:

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

第三种方法是使用操作员模块中的功能:

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

答案 3 :(得分:1)

您需要在调用之前定义函数。函数定义是Python中的可执行语句,由于你的无限循环,它们没有机会被定义。

您应该将四个定义移动到循环上方,此错误将消失。

更具风格的说明,您应该以可导入的方式构建模块,而不是仅仅运行。 Python程序使用的__name__ == "__main__"技巧是规范的方式,语言创始人的this article提供了如何正确构建它的一些见解。