1

I'm going through "Learn Python the Hard Way" by Zed Shaw.

I'm currently on lesson 36: http://learnpythonthehardway.org/book/ex36.html

My issue:

I'm trying to create my own game based on what Zed did in lesson 35: http://learnpythonthehardway.org/book/ex35.html

In his game he creates the following function to terminate the game:

def dead(why):
    print why, "Good job!"
    exit(0)

That's used in if/else statements such as this:

choice = raw_input("> ")

    if choice == "left":
        bear_room()
    elif choice == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")

When I run his program it exits when calling the dead function.

However, when I try and use this same construct in my own code I get the following error:

RunTime Error: Maximum Recursion Depth Exceeded

after lines of code spitting out the spot in my program where the exit(0) function is called.

Here's an example of how I have used it in my program:

def exit(message):
    print message, "Your adventure is over."
    exit(0)

With if/else statements like this:

answer = raw_input("> ")

if "stay" in answer:
    exit("Adventure and excitement are only available to those who participate.")
elif "don't" in answer:
    exit("Great things were never achieved by cowards.  Please rethink your choice.")
elif "wait" in answer:
    exit("Great things were never achieved by cowards.  Please rethink your choice.")
elif "outside" in answer:
    exit("Adventure and excitement are only available to those who participate.")
elif "enter" in answer:
    lobby()
elif "go" in answer:
    lobby()
elif "through" in answer:
    lobby()
elif "inside" in answer:
    lobby()
else: 
    exit("Well, you're no fun!")

It's not clear to me why it works in Zed's program but doesn't in my own. (And yes, I know that's probably an egregious use of elif's but my programming chops prohibit me from doing much else right now).

At first I thought it was due to how I was using while loops in my program but I've deleted all those and I'm having the same issues.

Thank you.

1 Answer 1

4

It's pretty simple. You named your function exit(), and your function tries to call a function named exit(). Thus your function calls itself, then calls itself again, and again, and again...

When a function calls itself, this is called a recursive call. If your function calls itself over and over too many times, eventually a limit is reached and Python stops your program. Python raises an exception, telling you that the "maximum recursion depth" was reached.

Here's a simple fix:

import sys

def dead(why):
    print why, "Good job!"
    sys.exit(0)

Now your program is explicitly asking for a particular exit() function, the one inside sys. Instead of calling itself recursively forever, it calls sys.exit() once and your program will stop.

Another solution is simply to rename your function to something other than exit(). Zed called his dead() which is why his example works.

Sign up to request clarification or add additional context in comments.

Comments

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.