NameError:name''未定义多个定义函数

时间:2017-03-10 17:18:22

标签: python

我想首先说我知道这段代码错了,我只是在测试

这是一个计算器,因为您可以从代码中看到我尝试将它们结束的数字作为结果,例如 10 + 10 = 20,他们将保持数字20并且可以继续20,我想继续重复该选项

代码:

def add(num1, num2):
    return num1 + num2

def mul(num1, num2):
    return num1 * num2

def sub(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1 / num2

def main():
    operation = input("Do you want to(+,-,*,/):  ")
    if(operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        print("That is an invalid operation")
    else:
        num1 = float(input("choose a number:  "))
        num2 = float(input("Choose another number:  "))
        if(operation == "+"):
            answer = (add(num1, num2))
            print(answer)
        elif(operation == "-"):
            answer = (sub(num1, num2))
            print(answer)
        elif(operation == "*"):
            answer = (mul(num1, num2))
            print(answer)
        elif(operation == "/"):
            answer = (div(num1, num2))
            print(answer)
        else:
            print("Syntax error!")
        def multiple(multiple):
             multiple =  input("would you like to carry the number(Y or N):  ")
            if(multiple == "Y" or multiple == "y"):
                carry = input("(+,-,*,/):  ")
                num3 = int(input("choose a number:  "))
                if(carry == "+"):
                    print(answer + num3)
                elif(carry == "-"):
                    print(answer - num3)
                elif(carry == "*"):
                    print(answer * num3)
                elif(carry == "/"):
                    print(answer / num3)
                else:
                    print("Syntax Error!")
    multiple = True
    while multiple == True:
        multiple()

choice = input("would you like multiple calculations? (Y or N):  ")
while(choice == "y" or choice == "Y"):
    main()
    multiple()
multiple()
main()

error message:

line 56, in <module>
    multiple()
NameError: name 'multiple' is not defined

p.s这可能会有一些缩进错误,因为它粘贴了奇怪的

2 个答案:

答案 0 :(得分:0)

您尝试在multiple函数范围之外调用函数main,而仅在其中定义它。假设您的缩进如此处所示,您需要将multiple的定义移到main之外,以便可以调用它。

此外,您正在定义一个名为multiple的变量,这可能会产生一些问题。您应该将其更改为其他内容。

答案 1 :(得分:0)

我对您的代码进行了一些优化并修复了它。它工作正常,所以看看它。

def add(num1, num2):
    return num1 + num2

def mul(num1, num2):
    return num1 * num2

def sub(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1 / num2

def main(carry):
    operation = input("Do you want to (+,-,*,/): ")
    if(operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        print("That is an invalid operation")
    else:
        num1 = float(input("choose a number: "))
        if carry == None:
            num2 = float(input("Choose another number: "))
        else:
            num2 = carry
        if(operation == "+"):
            answer = add(num1, num2)
        elif(operation == "-"):
            answer = sub(num1, num2)
        elif(operation == "*"):
            answer = mul(num1, num2)
        elif(operation == "/"):
            answer = div(num1, num2)
        print(answer)
        return answer

if input("would you like multiple calculations? (Y or N): ") in ("y", "Y"):
    domultiple = True
else:
    domultiple = False

carry = None
while 1:
    carry = main(carry)
    if domultiple:
        if input("would you like to carry the number (Y or N): ") in ("n", "N"):
            break
    else:
        break