0

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.

7
  • 1
    You are missing a 'if' statement in your code. Commented May 4, 2019 at 18:32
  • what actually you want this function to perform? Commented May 4, 2019 at 18:35
  • I want this function to add up the two values which the user input previously. Commented May 4, 2019 at 18:37
  • As i don't see if block, might be difficult to get correct answer. however based on the assumption we are calling this function inside if block. and if we are calling this in if block there looks to be scope issue. your defined function is not visible in else block and hence the error. Commented May 4, 2019 at 18:40
  • 1
    You are using calculation (argnum1, argnum2) without having defined argnum1, argnum2. This way, you are going to get NameError: name 'argnum1' is not defined. Commented May 4, 2019 at 18:47

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

You can move the function definition out of the while block.

def calculation():
    argnum1 = float(input("Enter your fist number: "))
    argnum2 = float(input("Enter your second number: "))
    result = argnum1 + argnum2
    print(result)
    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 == "+":
        calculation()

3 Comments

With this version the following error code occurs: TypeError: calculation() missing 2 required positional arguments: 'argnum1' and 'argnum2'
NameError: name 'argnum1' is not defined
remove that from the function call in 'else' as well as from the function definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.