user5715585
user5715585

Reputation:

RecursionError: maxium recursion depth exceeded

I'm new to stack overflow this is my second question so please bear with me. I'm creating a simple calculator program. Here's the code:

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y

print("what do you want to do?")
print("1.Add:")
print("2.Subtract:")
print("3.Multiply:")
print("4.Divide:")

choice = input("Enter a number 1-4 for your operation: ")

def operation():
    if choice == '1':
        print(num1,"+",num2,"=", add(num1, num2))

    elif choice == '2':
        print(num1,"-",num2,"=", subtract(num1, num2))

    elif choice == '3':
        print(num1,"*",num2,"=", multiply(num1, num2))

    elif choice == '4':
        print(num1,"/",num2,"=", divide(num1, num2))
    else:
        print("I SAID A NUMBER 1-4 YOU DUMBASS!")
        return(operation(), input())

operation()
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another one: "))

whenever I put a number greater than 5 it uses the else statement and loops the print statement over and over for a couple of seconds then print "recursion error: maximum recursion depth exceeded. I know that by putting the return statement inside the function, the function then loops itself over and over. I then tried to add the input to prompt the user for another input but I guess this isn't the right syntax. Can someone post the right syntax for this code or is there a more concise way of doing this? Thank you any help is appreciated.

Upvotes: 0

Views: 462

Answers (3)

Radhakrishna Pemmasani
Radhakrishna Pemmasani

Reputation: 766

There are two problems in your code, one is you are comparing a string with integer and the next one with out changing the value of option you are calling the same function, so it keeps on calling itself never ending, so the compiler giving you recursion error.

Upvotes: 0

mgilson
mgilson

Reputation: 309841

The problem is in your else clause:

else:
    print("I SAID A NUMBER 1-4 YOU DUMBASS!")
    return(operation(), input())

Notice that you call operation again, but you haven't changed the choice at all, so operation will run again with the same choice you had before (over and over until you hit the recursion limit).

At this point, you're probably tempted to do:

choice = input(...)
return operation()

However, that won't work either as choice is a global variable and if you try to modify it in the function, you'll get an UnboundLocalError.

You could declare global choice within operation, but that's suboptimal. A better design would be to pass the choice as an argument to operation:

choice = int(input("Enter a number 1-4 for your operation: "))

def operation(choice):
    if choice == 1:
        print(num1,"+",num2,"=", add(num1, num2))

    elif choice == 2:
        print(num1,"-",num2,"=", subtract(num1, num2))

    elif choice == 3:
        print(num1,"*",num2,"=", multiply(num1, num2))

    elif choice == 4:
        print(num1,"/",num2,"=", divide(num1, num2))
    else:
        print("I SAID A NUMBER 1-4 YOU DUMBASS!")
        choice = int(input())
        return operation(choice)

operation(choice)

Finally, even once you get this part working, you'll have additional problems depending on python2.x or 3.x. On python2.x, input will be returning numbers, not strings, so your equality tests (choice == '1') will never pass. On python3.x, the equality tests will work out just fine (because your choice will be a string), but the math equations will break (you can't multiply 2 strings together).

I've fixed that in the example above -- But remember not to use input in production code for python2.x as it's a huge security risk.

Upvotes: 3

Naihonn
Naihonn

Reputation: 1

That call is before input so it is calling and calling and calling again. This menu with choices is usually placed inside a while loop.

Upvotes: 0

Related Questions