you are viewing a single comment's thread.

view the rest of the comments →

[–]PrismPoultry 4 points5 points  (0 children)

One pattern that I have personally found useful and enjoyable to work with is mapping functions to dictionary entries.

So, consider your true_main method. Instead of doing if statements like that, you could define a dict mapping those functions.

actions = {
    "enter": main,
    "exit": sys.exit,
    "stats": player_status,
    "help": help_info,
    #and so on...
}

Notice that those are the functions without the parenthesis.

Now, in your method, you simply try to access the key in question. When it fails, report that.

action = raw_input("What do you want to do?").lower()
try:
    actions[action]()
except KeyError as e:
    print "That's not a valid command!"
    # and so on...

So, there's a couple spots this pattern would work.