python-3.xfunctionundefined

How to fix "undefined name" error message in Python?


I am creating a simple calculator with Python as my first "bigger" project. I am trying to use def function and when i am trying to call that function it gives "undefined name" error message.

while True:
    print ("Options: ")
    print ("Enter '+' to add two numbers")
    print ("Enter '-' to subtract two numbers")
    print ("Enter '*' to multiply two numbers")
    print ("Enter '/' to divide two numbers")
    print ("Enter 'quit' to end the program")
    user_input = input(": ")
        
    def calculation (argnum1, argnum2):
        argnum1 = float (input("Enter your fist number: "))
        argnum2 = float (input("Enter your second number: "))
        number = argnum1
        number = argnum2
        result = argnum1 + argnum2
        print (result)
        print("-"*25)
        return number
        return result

    if user_input == "quit":
        break
            
    elif user_input == "+":
        calculation (argnum1, argnum2)

I expect the output of argnum1 + argnum2 result.


Solution

  • You have needlessly defined your function to take two parameters, which you cannot provide as they are defined inside the function:

        def calculation (argnum1, argnum2):  # argnum1 and argnum2 are immediately discarded
            argnum1 = float (input("Enter your fist number: "))  # argnum1 is defined here
            argnum2 = float (input("Enter your second number: "))
            # do things with argnum1 and argnum2
        ...
        calculation(argnum1, argnum2)  # argnum1 and argnum2 are not defined yet
    

    Note that the body of a function is executed only when the function is called. By the time you call calculation, argnum1 and argnum2 are not defined - and even then, they only get defined in another scope.

    Ideally, move the input call outside of your function:

        def calculation (argnum1, argnum2):
            # do things with argnum1 and argnum2
        ...
        argnum1 = float (input("Enter your fist number: "))  # argnum1 is defined here
        argnum2 = float (input("Enter your second number: "))
        calculation(argnum1, argnum2)
    

    Note that you should define your function outside the loop. Otherwise, it is needlessly redefined on every iteration. There is also no point in having multiple return statements after one another.

    Your code should look like this:

    def add(argnum1, argnum2):
        result = argnum1 + argnum2
        print (result)
        print("-"*25)
        return result
    
    while True:
        print ("Options: ")
        print ("Enter '+' to add two numbers")
        print ("Enter '-' to subtract two numbers")
        print ("Enter '*' to multiply two numbers")
        print ("Enter '/' to divide two numbers")
        print ("Enter 'quit' to end the program")
        user_input = input(": ")
    
    
        if user_input == "quit":
            break
        elif user_input == "+":
            argnum1 = float (input("Enter your fist number: "))
            argnum2 = float (input("Enter your second number: "))
            add(argnum1, argnum2)